Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Loop Break

by ww (Archbishop)
on Jan 19, 2012 at 16:18 UTC ( #948804=note: print w/replies, xml ) Need Help??

in reply to Loop Break

Angle_brackets don't work that way.

To get an array of filenames, see open, opendir Duh! Sorry, readdir, or any number of threads here (hint: Super Search with terms like "dir files" or similar).

Of course, you could make your list with

ls -la /dirpath/dir/ > file_of_names
dir /a /dirpath/dir/ > file_of_names
But this is, after all, a perl forum.

Moving on, your regex would (if the script worked) catch files with names like pqr.cobol so you might consider this:

#!/usr/bin/perl use Modern::Perl; use Diagnostics; my (@file, $file, $filename); @file = <DATA>; for $file (@file) { if($file =~ /\.c$/i) { # match only filenames ending in ".c" or ". +C" $filename = $file; print "$filename\n"; } } __DATA__ bzz.c bzz.doc pqr.cobol 123.1c def.C foo.endswithC

For the record, though, your regex does achieve a good many proper exclusions

bzz.doc             \
123.1c               # not matched by your regex, ++
foo.endswithC /
Update: fixed brain-fart in para 1

Replies are listed 'Best First'.
Re^2: Loop Break
by JavaFan (Canon) on Jan 19, 2012 at 16:21 UTC
    Angle_brackets don't work that way.
    They do.
    perldoc -f glob
      ++ for your reply, JavaFan because I was misreading this (from the glob doc you cited):
      Note that "glob" splits its arguments on whitespace and treats each segment as separate pattern. As such, "glob("*.c *.h")" matches all files with a .c or .h extension. The expression "glob(".* *")" matchs all files in the current working directory.

      Nothing there bars this:

      #!/usr/bin/perl use Modern::Perl; my @arr = glob("*.*"); for my $line(@arr) { say $line; }

      Also, as noted by Corion, perlop is relevant.

      If what the angle brackets contain is a simple scalar variable (e.g., +<$foo>), then that variable contains the name of the filehandle to in +put from, or its typeglob, or a reference to the same. For example: $fh = \*STDIN; $line = <$fh>; If what's within the angle brackets is neither a filehandle nor a simp +le scalar variable containing a filehandle name, typeglob, or typeglo +b reference, it is interpreted as a filename pattern to be globbed, a +nd either a list of filenames or the next filename in the list is ret +urned, depending on context.

      Above blockquote added as an update, for future readers

      TY, both!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://948804]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2018-05-20 14:52 GMT
Find Nodes?
    Voting Booth?