Re: using file globs and regex
- From: pgodfrin <pgodfrin@xxxxxxxxx>
- Date: Fri, 7 Aug 2009 12:21:52 -0700 (PDT)
OK - I figured it out - thanks.
This works fine: my($fn2) = grep ( $_ =~ '6' , glob 'f*');
What I'm confused about is the parens around the $fn2 ? Why are those
needed? Something tells me that has something to do with making the
scalar $fn2 into a list 'cause that's what grep returns, but i can't
remember what that's called to look it up.
help?
pg
On Aug 7, 1:23 pm, pgodfrin <pgodf...@xxxxxxxxx> wrote:
hmmm - what if I wanted to do some search on the glob?
like my($fn) = grep ( /5/ , glob 'f*');
?
pg
On Aug 7, 12:23 pm, pgodfrin <pgodf...@xxxxxxxxx> wrote:
Thanks Tad - works like a charm...
pg
On Aug 7, 12:06 pm, Tad J McClellan <ta...@xxxxxxxxxxxxxx> wrote:
pgodfrin <pgodf...@xxxxxxxxx> wrote:
Greetings,
This program works fine (with file names f1..f8 in the directory):
#!/usr/bin/perl
use warnings;
use strict;
$\="\n";
while(<f*>)
{
print "$_";
if(/f5/)
{
my $fn=$_;
print "File name $fn found..." and exit;
}
}
exit;
But I was hoping to be able to do something like:
my $fn=grep (/f5/,<f*>);
Basically just trying to have less lines of code...any suggestions?
use grep() in a list context instead of in a scalar context.
I don't like using overloaded angle brackets in my code.
If I want equality, I use an operator that tests for equality.
my($fn) = grep ( $_ eq 'f5' , glob 'f*');
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
.
- Follow-Ups:
- Re: using file globs and regex
- From: Tad J McClellan
- Re: using file globs and regex
- From: Steve C
- Re: using file globs and regex
- From: Ben Morrow
- Re: using file globs and regex
- References:
- using file globs and regex
- From: pgodfrin
- Re: using file globs and regex
- From: Tad J McClellan
- Re: using file globs and regex
- From: pgodfrin
- Re: using file globs and regex
- From: pgodfrin
- using file globs and regex
- Prev by Date: FAQ 8.13 How do I trap control characters/signals?
- Next by Date: Re: Perl process as a unix background process
- Previous by thread: Re: using file globs and regex
- Next by thread: Re: using file globs and regex
- Index(es):