Joel R Stout writes ..


>I want to get the file names of all "EDI" files from a certain 
>directory.  I
>do not want "ENT" (or encrypted) file names.  
>
>Example:
>
>The directory contains:
>a001.edi
>a002.edi
>a003.edi.ent
>a004.EDI
>
>After getting the file name in $_ I did the following match:
>
>if (/edi\b/i) { # thinking I would match file names 1, 2, and 
>4.  Instead I
>matched on all.  Wasn't \b supposed to help me out here?  Or does Perl
>consider "." in this case a word boundary?  Any help much appreciated.


while others have correctly answered your question about "\b" I just wanted
to take the time to show the idiomatic way of reading in files from a
directory

  my $directory = '.';  # or whatever it is

  opendir DIR, $directory or die "Bad opendir: $!";

  my @filename =
    grep { -f "$directory/$_" && /\.edi$/i && /^[^.]/ } readdir DIR;

  closedir DIR;


you can see the use of 'grep' here with the code block format .. it
basically turns into a filter ensuring that anything that comes out of the
readdir statement returns true for all the tests

the first test just makes sure it's a plain file and not a directory or
symbolic link .. the second test is your one - it makes sure that the
filename ends in '.edi'

the third test was not in your specification but is a common one to include
. it filters out any UNIX hidden filenames (ones that begin with the
period)

obviously you can put whatever tests you like in there .. some people even
go so far as to then map the output of the 'grep' to include the directory
name (because the 'readdir' only returns filenames - not pathnames) .. eg.

  my @pathname = map { "$directory/$_" }
    grep { -f "$directory/$_" && /\.edi$/i && /^[^.]/ } readdir DIR;

hope that helps you and others


references:

  perldoc -f grep
  perldoc -f map

-- 
  jason king

  At one time in Wisconsin, all yellow butter substitutes were banned
  in the state, which people then smuggled in from Illinois. -
  http://dumblaws.com/

Reply via email to