using file globs and regex

P

pgodfrin

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?
phil g
 
P

pgodfrin

Thanks Tad - works like a charm...
pg

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*');
 
P

pgodfrin

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
 
S

Steve C

- SC.

then it asks grep for a scalar.
but when you call it without the parens
it asks grep for a list
When you put $fn2 inside the parens then
It's called list context.
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
 
X

Xho Jingleheimerschmidt

Tad said:
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*');

Or just don't remove the equality in the first place.

my($fn) = glob 'f5';

Although it seems like a file test operator would make more sense here.

Xho
 
P

pgodfrin

[please don't top-post]

Quoth pgodfrin said:
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.

    my $x = ...

evaluates the RHS in scalar context, but

    my ($x) = ...

evaluates it in list context. The same applies without the 'my', or with
'local' instead. This is one of the few cases where explicit parens make
a difference in deciding whether something is a list or not.

Ben

Nice to hear from you Ben (hope things are well).
Thanks Ben and Tad for clearing up the 'context'.
Of course, you're right Tad - the code assumes the first one it finds
is the right one. In this case that should be ok - but knowing more
about that behavior is a good thing (and might keep me from posting
some other day).

(sorry about the top post)
pg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top