On Nov 28, 2003, at 8:54 PM, Jason Dusek wrote:
Hi Everyone,
On Friday, November 28, 2003, at 08:51 PM, drieux wrote:a. how did you initialize it to begin with and why not simple re-use that solution
The hash consists of filenames, line numbers and strings.
$HASH{$file}{$line} = line of code from some file.
So the script goes along scouring the present directory for all instances of files of type '*.foo' and when it comes to one, it searches every last line of it for the pattern /\bfoo\b/ and if (and only if) there is a foo in the file, the it puts the name of the file in the hash, and then puts the number of each line containing /\bfoo\b/ in the hash under the file's name, and then puts the line of code containing /\bfoo\b/ under the line number. Then it prints out the hash and tells me what directory it was in when it did all this.
So this is all fine well and good. But let us say that I wish to rewrite the script so that it scours many directories in sequence, and then tells me what it did in each one. There is only one thing that needs to be changed - each time it comes to a new directory, it should wipe the hash. Then each directories report will not be contaminated by the previous report. If I were to simply use the way I had initialized the hash in the first place, I would get a very long report for the last directory, most of which would be about files in other directories! So I need to nuke the hash each time I go through the loop.
maybe I'm not seeing why you want to have a HoH in the first place, I mean if all you want is
$HASH{$file}{$line_number} = $specific_instance
why not modify your notion of $file from 'name of file' to say FQN - fully qualified name.
shifting from bob.c to foo/bar/bob.c
eg:
$HASH{'foo/bar/bob.c'}{'123'} = 'and lo there is foo in this code';
Remember the other side of the classic unix philosophy, you malloc from the top, and you free from the bottom, so were you to have some hash of the form
$hash{$foo}{$bar}{$baz}
you would want to recurse down the structure to throw away the leafs first so that one frees them up, so that there are no dangling references.
I of course am a fan of Hash Refs, lifestyle issue, hence think in terms of say:
my $hash = { foo => { bar => { bar => { thingie => 'a' } }, bar2 => { thingie => 'aq' , b2 => 'other' } }, bar => { baz => { wombat => 'a1', frodo => 'baggins'}, bazBall => {bobo => 'b2', fep => 'friend'} }, bing => '1', bong => ['a', 'b', 'c' ], }; print Dumper($hash);
free_leaf($hash); print Dumper($hash); #------------------------ # sub free_leaf { my ($leaf) = @_; my $ref = ref($leaf); return unless( $ref); if ($ref eq 'HASH') { while( my ($k,$v) = each %$leaf ) { free_leaf($v); delete($leaf->{$k}); } } elsif ( $ref eq 'ARRAY' ) { while( my $token = shift @$leaf ) { free_leaf($token); } } } # end of free_leaf
ciao drieux
---
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]