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

I am getting the following warning while running my code

Use of uninitialized value in pattern match (m//) at orphan_plf.pl line 37

and line 37 is $Hash_filematches{$filename}= grep( /\/\Q$file_name\E#/i, $Hash_filenames{$plf} ); The objective of my program

1.Get all the filenames with .plf extension from a directory

2.Store the filename as key and the corresponding lines of it as values in a hash

3.For every filename in the hash saved in step 2 grep for a filename in the corresponding lines and save the filename and the corresponding match in another hash and them print the hash

Please help
#!/usr/bin/perl -w use strict; use warnings; my %Hash_filenames=(); my %Hash_filematches=(); my $plf; my $start_dir; my $start_dir = \\server1\tools ; my $file_name = "load.c"; opendir(DIR, "$start_dir"); my @plf_files = grep(/\.plf$/,readdir(DIR)); print "PLF FILES\n"; print "@plf_files\n"; foreach my $plf (@plf_files) { chomp($plf); open my $match, '<',"$start_dir\\$plf" or die "could not open '$plf' $ +!"; my @file_lines = <$match>; #Save the filename as key and the lines in the file as value in a hash + $Hash_filenames{$plf}=@file_lines; } closedir(DIR); if ($file_name) { foreach my$filename(keys %Hash_filenames) { #for every key(filename) in the above hash namely #$Hash_filenames #grep for $file_name in the #corresponding values (lines in the file)of the file #and save the match as value and key as filename #in another hash $Hash_filematches{$filename}= grep( /\/\Q$file_name\E#/i, $Hash_filen +ames{$plf} ); } } { local $, = "\n"; print "PRINTING MATCHED HASHES"; foreach my $key ( keys %Hash_filenames ) { my $value = $Hash_filematches{$key}; print "$key => $value\n"; } }
  • Comment on Grepping for a value in a hash of filenames and printing the matches
  • Download Code

Replies are listed 'Best First'.
Re: Grepping for a value in a hash of filenames and printing the matches
by jwkrahn (Abbot) on Oct 27, 2010 at 06:35 UTC

    Change:

    $Hash_filenames{$plf}=@file_lines;

    To:

    $Hash_filenames{ $plf } = \@file_lines;

    And change:

    $Hash_filematches{$filename}= grep( /\/\Q$file_name\E#/i, $Hash_filen +ames{$plf} );

    To:

    @{ $Hash_filematches{ $filename } } = grep /\Q/$file_name\E#/i, @{ $Ha +sh_filenames{ $plf } };
      Thanks for the reply.

      I am getting "Use of uninitialized value in hash element at orphan_plf.pl line 40" and line 40 is @{ $Hash_filematches{ $filename } } = grep( /\/\Q$file_name\E#/i, @{ $Hash_filenames{ $plf } });

      and the output is PRINTING MATCHED HASHES apps.plf => ARRAY(0x1981c80) modem.plf => ARRAY(0x1981ca4) ...........

      1.I am confused on why apps.plf is showing beacause the file we are grepping is not in this PLF. 2.the output is printing the reference values,how do I fix this?Please help

Re: Grepping for a value in a hash of filenames and printing the matches
by ikegami (Patriarch) on Oct 27, 2010 at 07:20 UTC

    $Hash_filenames{$plf}=@file_lines;

    An array evaluated in scalar context returns the number of elements it contains.

    A hash element is a scalar. It cannot contain an array. It can contain a reference to an array.

    Fix:

    my @file_lines = <$match>; $Hash_filenames{$plf} = \@file_lines;

    or

    $Hash_filenames{$plf} = [ <$match> ];

    $Hash_filematches{$filename}= grep( /\/\Q$file_name\E#/i, $Hash_filenames{$plf} );

    • $Hash_filenames{$plf} is a scalar, so your grep is going to return at most one item.
    • You call grep in scalar context, so it returns the number of matching items rather than the items themselves.
    • You never assigned a value to $plf.

    Fix for the first two:

    $Hash_filematches{$filename} = [ grep( /\/\Q$file_name\E#/i, @{ $Hash_filenames{$plf} } ) ];

    Sorry, I don't have time to understand what you are trying to do in order to propose a solution for the third item. But maybe knowing what the problem is will help you.

Re: Grepping for a value in a hash of filenames and printing the matches
by suhailck (Friar) on Oct 27, 2010 at 05:52 UTC
    Are you trying to achieve something which can be easily done using grep in *nix systems

    grep -R  'pattern'  *.plf
Re: Grepping for a value in a hash of filenames and printing the matches
by umasuresh (Hermit) on Oct 27, 2010 at 12:52 UTC
     use Data::Dumper to see what is in your Hash or other complex Data Structure.