iphone has asked for the wisdom of the Perl Monks concerning the following question:
Hi, I am pasting a snippet of my code. The below code get multiple files ($file_name) and then grep for each file_name in the values of a hash(Hash_filenames) and then save the grep that matches in another hash(Hash_filematches).The issue is only the data of the last file_name is getting saved.How do I fix it?
while (my $line = <$DATA>){ (my $file_name) = $line =~ / ........... if ( ($file_name) and ( !$seen_file{$file_name}++ ) ) { foreach my $filename(keys %Hash_filenames) { @{ $Hash_filematches{ $filename } } = grep( /\/\Q$file_name\E#/i +, @{ $Hash_filenames{ $filename } });--------->@{ $Hash_filematches{ +$filename } } stores only the grep of the last $file_name }#for loop end }#if file_name end .... for my $key (keys %Hash_filematches) { my $value = $Hash_filematches{$key}; if (scalar @$value) { # check that the arrayref isn't empty print "KEY: $key\n"; print "VALUES: ", join(", ", @$value), "\n\n"; } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Only the last file data is getting saved in the hash
by biohisham (Priest) on Nov 07, 2010 at 07:47 UTC | |
by iphone (Beadle) on Nov 07, 2010 at 09:43 UTC | |
by Anonymous Monk on Nov 07, 2010 at 12:02 UTC | |
|
Re: Only the last file data is getting saved in the hash
by Anonymous Monk on Nov 07, 2010 at 07:28 UTC | |
| |
|
Re: Only the last file data is getting saved in the hash
by kcott (Archbishop) on Nov 07, 2010 at 07:25 UTC | |
by Anonymous Monk on Nov 07, 2010 at 07:29 UTC | |
by iphone (Beadle) on Nov 07, 2010 at 07:45 UTC |