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

27483Re: [PBML] What does this mean?

Expand Messages
  • Shlomi Fish
    Oct 27, 2013
    • 0 Attachment
      Hi Peter,

      thanks for replying to the list and for not top-posting.

      On Sun, 27 Oct 2013 12:56:00 -0400
      Peter Holsberg <pjh42@...> wrote:

      > Thank you, Shlomi,
      >
      > I just joined. However, I do have a response to what you so kindly wrote.
      >

      Nice.

      > Shlomi Fish has written on 10/27/2013 1:28 AM:
      > >
      > >
      > > 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';
      > > »»»
      >
      > You were making a joke, right?

      No, I was not. :-(

      > Perhaps I was clumsy in the way I asked
      > the question.
      >
      > Now that I know that since
      >
      > my $file = File::Spec->catfile($dir, $files[0]);
      >
      > gets the path to the file inside the directory path of "$dir" and the
      > basename of "$files[0]" and puts it in $file, what I would like is that
      > $file simply be the value of $files[0] -- if "basename" means everything
      > after the last directory separator character, e.g., the name and the
      > extension.
      >
      > Am I right?

      OK then do:

      «««
      my $file = $files[0];
      »»»

      You can also do «my $file = shift(@files);» but this will mutate @files. See
      http://perldoc.perl.org/functions/shift.html .

      >
      >
      > >> 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 the 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 .
      >
      > Wow! All in one command!!!

      That's the power of composition. It's not specific to Perl but to many
      programming languages in general.

      > I would have trouble getting that result in
      > multiple commands! Quite a newbie, aren't I? :-)
      >
      > The "\A" puzzles me a little. Does it mean that it skips all characters
      > until a match is found??

      No, "\A" matches at the beginning of the string (whereas "^" matches at the
      beginning of the line or the beginning of the string depending on the
      presence or lack of it of the /m flag). If the regular expression is not
      anchored, it would match everywhere -
      «"The grass is always greener" =~ /grass/» => true.

      >
      >
      > > There are some recommended resources for learning Perl here:
      > >
      > > * http://perl-begin.org/
      > >
      > > * http://perl-tutorial.org/
      >
      >
      > Thanks again for your reply.
      >

      You're welcome.

      -- Shlomi Fish

      --
      -----------------------------------------------------------------
      Shlomi Fish http://www.shlomifish.org/
      Interview with Ben Collins-Sussman - http://shlom.in/sussman

      The NSA employs the largest number of mathematicians with Ph.Ds. And the most
      stupid and incompetent ones.
      http://www.shlomifish.org/humour/bits/facts/NSA/

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