Re: using file globs and regex



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/"

.



Relevant Pages

  • Re: using file globs and regex
    ... and exit; ... use grepin a list context instead of in a scalar context. ... If I want equality, I use an operator that tests for equality. ... Although it seems like a file test operator would make more sense here. ...
    (comp.lang.perl.misc)
  • Re: using file globs and regex
    ... use strict; ... and exit; ... use grepin a list context instead of in a scalar context. ... If I want equality, I use an operator that tests for equality. ...
    (comp.lang.perl.misc)
  • Re: mech->content match regex howto
    ... raphael() wrote: ... Why am I not able to use a regex on it in scalar form? ... I think the above one is "wrong syntax" for using list context? ... Also how can I make grep work? ...
    (perl.beginners)
  • Re: Forcing list context on <$fh>
    ... If you put the params into a hash then you can use a "hash slice" ... A test for equality should *look like* a test for equality: ... Is there a nice way to force list context on it? ... There is no way to "force" list context like there is for scalar context. ...
    (comp.lang.perl.misc)
  • Re: while grep filehandle
    ... read perldoc -f grep to understand what grep does in a scalar context. ... here is being called in a list context. ...
    (comp.lang.perl.misc)