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

27481Re: [PBML] What does this mean?

Expand Messages
  • Shlomi Fish
    Oct 26, 2013
      Hello Peter,

      please note that it is preferable to use the beginners@... mailing list:

      http://learn.perl.org/faq/beginners.html

      anyway, to answer your questions (see below):

      On Sat, 26 Oct 2013 13:02:45 -0400
      Peter Holsberg <pjh42@...> wrote:

      > I'm trying to understand a script someone else wrote so I can modify it
      > for my own purposes.
      >
      > I'm trying to read (and modify) a file in the current directory. The
      > filename has 6 digits (eg., 131026) and is of type .htm. Other files
      > that may be used are similarly named, for example,
      >
      > 131026.htm
      > 131026.aca
      > 131026.pbn
      > 131026.bws
      > 131026.txt
      > 131026ARECAP.txt
      >
      > The script begins:
      >
      > #!/usr/bin/perl
      >
      > use strict;
      > use warnings;
      >
      > use Tie::File;
      > use File::Copy 'copy';
      > use File::Spec;
      >
      > use Cwd;
      > my $dir = cwd;
      >
      > opendir my ($dh), $dir;
      >
      > # Find all files in the directory that match \d{6}\.htm
      > # Die if none or multiple files found
      > #
      >
      > my @files = grep /\A\d{6}\.htm\z/i, readdir $dh;
      > die 'No matching files found' unless @files;
      > die 'Multiple matching files found' if @files > 1;
      > my $file = File::Spec->catfile($dir, $files[0]);
      >
      > print "\n\n------------ FOUND THIS: $file \n\n";
      >
      > The output of the above command is:
      > E:\_WEBPAGES\.Cinna\ACBLmerge\131021.htm
      >
      > I think that everything that follows would be much simpler if $file had
      > the value 131021.htm
      >
      > So, a couple of questions:
      >
      > 1. How do I operate on $file so get what I want?
      >

      Add this statement (without the « and »):

      «««
      $file = '131021.htm';
      »»»

      > 2. Can someone please parse
      >
      > my @files = grep /\A\d{6}\.htm\z/i, readdir $dh;
      >

      This calls http://perldoc.perl.org/functions/readdir.html in list context on
      $dh to retrieve the list of filenames contained in tihe directory, and then
      matches filename with 6 digits followed by the ".htm" extension and extracts
      only them (using http://perldoc.perl.org/functions/grep.html ) and puts them
      into the array variable @files .

      > and
      >
      > my $file = File::Spec->catfile($dir, $files[0]);
      >

      This gets the path to the file inside the directory path of "$dir" and the
      basename of "$files[0]" and puts it in $file.

      There are some recommended resources for learning Perl here:

      * http://perl-begin.org/

      * http://perl-tutorial.org/

      Regards,

      Shlomi Fish

      --
      -----------------------------------------------------------------
      Shlomi Fish http://www.shlomifish.org/
      Perl Humour - http://perl-begin.org/humour/

      Vizzini: He didn’t fall?! Inconceivable!
      Inigo Montoya: You keep using that word. I do not think it means what you
      think it means.
      http://en.wikiquote.org/wiki/The_Princess_Bride_%28film%29

      Please reply to list if it's a mailing list post - http://shlom.in/reply .
    • Show all 4 messages in this topic