in reply to Re: How to access the hash array values?
in thread How to access the hash array values?

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 }

Replies are listed 'Best First'.
Re^3: How to access the hash array values?
by Gulliver (Monk) on Apr 30, 2011 at 20:36 UTC

    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.

Re^3: How to access the hash array values?
by cdarke (Prior) on May 01, 2011 at 06:57 UTC
    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.