Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Only the last file data is getting saved in the hash

by biohisham (Priest)
on Nov 07, 2010 at 07:47 UTC ( [id://869872]=note: print w/replies, xml ) Need Help??


in reply to Only the last file data is getting saved in the hash

iphone post a self contained boil-down of this code that actually works and demonstrates the problem you're experiencing rather than a snippet that needs fixing..

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?


Excellence is an Endeavor of Persistence. A Year-Old Monk :D .

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

    Not certain but may be i should save the data below in an array reference,not in an array??

    @{ $Hash_filematches{ $filename } } = grep( /\/\Q$file_name\E#/i +, @{ $Hash_filenames{ $filename } });
      Not certain but may be i should save the data below in an array reference,not in an array??

      You already are

      $ perl -MData::Dump::Streamer -e" $f{f} = [ grep /\d/ , 1 ..4 ]; Dum +p( \%f ); " $HASH1 = { f => [ 1, 2, 3, 4 ] }; $ perl -MData::Dump::Streamer -e" @{ $f{f} } = grep /\d/ , 1 ..4; Dump +( \%f ); " $HASH1 = { f => [ 1, 2, 3, 4 ] };

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://869872]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-03-29 02:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found