Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have a hash whose values are an array of file contents.I am trying to get a for loop going for each line the value as below.For some reason the for loop is not entered.When I debugged,the array value is like below.Can someone advise what am I doing worng?

@{$grep_lines{$filename}}= ARRAY(0x...) my %grep_lines; foreach $mk(@{ $grep_lines{ $filename } }) { #doesnt enter the loop }

Replies are listed 'Best First'.
Re: How to access the hash array values?
by chromatic (Archbishop) on Apr 30, 2011 at 18:51 UTC

    Can you post your actual code? What you've given won't compile, so the best anyone can do is to guess.

      Full code below

      oem.min contains SRCPATH = "${BUILD_ROOT}/multimedia/graphics/apps/OEM/src" #!/usr/bin/perl -w use strict; use warnings; my @folders= ("oem"); my @array=("./oem.min"); my $folder; my %files_content; my %grep_lines; my ($file,$filename,$mk); foreach $file (@array) { chomp($file); open my $fh, '<',"$file" or die "could not open '$file' $!"; my @file_lines = <$fh>; $files_content{$file}=\@file_lines; close $fh; } foreach $folder (@folders) { foreach $filename (keys %files_content) { push @{ $grep_lines{ $filename } }, grep (/\b$folder\b/i, @{$files_c +ontent{ $filename } }); } } foreach $mk (@{ $grep_lines{ $filename } }) { print "\nIn the loop\n";#Doesnt enter the loop }

        When I run the code it dies with this error message:

        Use of uninitialized value $filename in hash element at 902239.pl line 29.

        $filename loses the value assigned to it at the end of the previous foreach block. Try putting in some print statements and use data::dumper to verify the contents of the array after the loop that assigns to it.

        In your final foreach loop $filename has not been set to anything. You define it near the top of the script, but do not set it to any value.

        You might be thinking that the inner foreach loop that uses $filename as a loop variable would set it, but it will not. A foreach loop variable is not a 'real' variable, but an alias to each element in the list in turn. It is not accessible outside the loop. Even so, your logic is a bit suspect, and I suggest you might have meant the final loop to be inside the folder loop.
Re: How to access the hash array values?
by Khen1950fx (Canon) on May 01, 2011 at 11:01 UTC
    I tried your script in another way. I condensed it and used the List::MoreUtils each_arrayref method and Data::Dumper. It'll give you a .h file and its contents; however, if you'd rather just get the first line of the files, just replace "cat" with "head -1".
    #!/usr/bin/perl use strict; use List::MoreUtils qw(each_arrayref); use Data::Dumper::Concise; my @arr = ( '/root/Desktop/data/data.h', '/root/Desktop/power/power.h'); open STDOUT, '>', '/root/Desktop/arr.log'; my $iterator = each_arrayref(\@arr); grep{-f} glob( <*.h> ); foreach my $line(my @lines = &$iterator()) { print Dumper($arr[0]); print Dumper(system("cat $arr[0]")); print Dumper($arr[1]); print Dumper(system("cat $arr[1]")); } close STDOUT;