Loading ...
Sorry, an error occurred while loading the content.

print the lines that match a file name in an array of files

Expand Messages
  • GoPi
    I am dividing my program into three parts.I need your help in each one of them.Pls help Objective my script is to print the lines that match a file name in an
    Message 1 of 2 , Oct 25, 2010
    • 0 Attachment
      I am dividing my program into three parts.I need your help in each one of them.Pls help
      Objective my script is to print the lines that match a file name in an array of files.

      1.Getting all the files with .plf extension in a directory
      #!/usr/bin/perl -w
      use strict;
      use warnings;
      %Hash_filenames={};
      $file name = "Data";
      $start_dir = "C\:\\Program Files";
      my @plf_files = <"$start_dir/*.plf">;
      print "PLF FILES\n";
      print "@plf_files\n"; ---->Doesn't print anything.


      2.Storing the filename and corresponding lines in a hash with filename as key and lines as the value
      foreach my $plf (@plf_files) {

      open my $match, '<',$plf or die "could not open '$plf' $!"

      $Hash_filenames{'filename')=$plf;
      $Hash_filenames{'lines')=$match;
      }

      3.This is the diffcult part and am not start here.Need your valuable inputs here.

      I want to grep for $file name in $Hash_filenames{'lines'} for every $Hash_filenames{'filename'} and then store the matches in another hash where filename is the key and the corresponding matches as the value.

      .Finally printing the Hash values corresponding to every filename.
    • Shlomi Fish
      Hi GoPi, ... Match a filename how? ... 1. This won t compile as you haven t declared %Hash_filenames and you re using strict and warnings . Did you actually
      Message 2 of 2 , Oct 26, 2010
      • 0 Attachment
        Hi GoPi,

        On Tuesday 26 October 2010 08:30:47 GoPi wrote:
        > I am dividing my program into three parts.I need your help in each one of
        > them.Pls help Objective my script is to print the lines that match a file
        > name in an array of files.

        Match a filename how?

        >
        > 1.Getting all the files with .plf extension in a directory
        > #!/usr/bin/perl -w
        > use strict;
        > use warnings;
        > %Hash_filenames={};

        1. This won't compile as you haven't declared %Hash_filenames and you're using
        "strict" and "warnings". Did you actually try running the program?

        2. You should initialise %hash_filenames (please don't use uppercase letter
        unnecessarily) using either "()" (or nothing at all as a hash is empty by
        default) - not "{}" which is the anonymous hash-ref constructor.

        > $file name = "Data";

        Again this won't compile - 1) You don't have a "my". 2) whitespaces are not
        recognised by Perl as part of the variable name (unless you do some ugly
        tricks with package-scope variables).

        There are more problems like that below. Please fix all the compilation
        problems and return to us. See http://perl-begin.org/ for more guidance about
        Perl.

        Regards,

        Shlomi Fish

        --
        -----------------------------------------------------------------
        Shlomi Fish http://www.shlomifish.org/
        My Public Domain Photos - http://www.flickr.com/photos/shlomif/

        <rindolf> She's a hot chick. But she smokes.
        <go|dfish> She can smoke as long as she's smokin'.

        Please reply to list if it's a mailing list post - http://shlom.in/reply .
      Your message has been successfully submitted and would be delivered to recipients shortly.