in reply to Only the last file data is getting saved in the hash
Another notice, in the first line while(<$DATA>), you don't really want to use names similar to special literals like DATA as your filehandle names, that may lead to confusion in case someone took up to maintain your code since DATA is connected with the special token __DATA__, read perldata->special literals and SelfLoader.
Where does your while loop end? How did the %Hash_filenames come to existence? Depending on a test sample of your file names that you're searching, what do you expect each one of the hashes you've used to contain?
When you say only the last file 'data' is getting saved in the hash, is 'data' reflective of the file name or the file contents? in both cases however, you may be experiencing this because something is getting overwritten with every iteration of the loop ...
I tried cleaning up your code a little bit to get it working but I don't have any parameters to reproduce the behavior you're describing
#!/usr/local/bin/perl use strict; use warnings; use Data::Dump qw/pp/; my %seen_file; my %Hash_filematches; my %Hash_filenames; while ( chomp(my $line = <DATA>)){ (my $file_name) = $line; if ( ($file_name) and ( !$seen_file{$file_name}++ ) ) { foreach my $filename(keys %Hash_filenames) { print "FILE NAME $file_name\n"; @{ $Hash_filematches{ $filename } } = grep( /\/\Q$file_name\E#/i +, @{ $Hash_filenames{ $filename } }); #@{ $Hash_filematches{ $filename } } = grep( (/\/\Q$file_name\E#/ +i && !/\.plf/), @{ $Hash_filenames{ $filename } });--------->@{ $Hash +_filematches{ $filename } } stores only the grep of the last $file_na +me }#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"; } } } print pp(\%seen_file); #print pp(\%Hash_filematches); #print pp(\%Hash_filenames); __DATA__ file1 file2 file2 file3
Take it from here and read How di I post a question effectively?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Only the last file data is getting saved in the hash
by iphone (Beadle) on Nov 07, 2010 at 09:43 UTC | |
by Anonymous Monk on Nov 07, 2010 at 12:02 UTC |