in reply to How to access the hash array values?

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

  • Comment on Re: How to access the hash array values?

Replies are listed 'Best First'.
Re^2: How to access the hash array values?
by Anonymous Monk on Apr 30, 2011 at 19:25 UTC

    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.