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 .

In reply to Re: Only the last file data is getting saved in the hash by biohisham
in thread Only the last file data is getting saved in the hash by iphone

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.