Searching each element of an array with grep

K

KaZ

Hello,

I'm trying to find all elements of an array that match a particular
pattern. So far, I used:

grep { /\t$var\t/ } @array

but it doesn't match. I have the impression, it tries to match only the
element of the array which are *exactly* matching. But I want a match
anywhere in any element of the array.

Here:
http://perldoc.perl.org/functions/grep.html
there is a "!" before the "/". What does this means? I didn't find
anything about this...

I also tried /.*\t$var\t.*/ but it doesn't match either...

Greetings,
 
X

xhoster

KaZ said:
Hello,

I'm trying to find all elements of an array that match a particular
pattern. So far, I used:

grep { /\t$var\t/ } @array

but it doesn't match.

Of course it doesn't. @array is empty. But then again, even if the regex
did match, how would you know? The grep is in a void context.
Here:
http://perldoc.perl.org/functions/grep.html
there is a "!" before the "/". What does this means? I didn't find
anything about this...

It is an ordinary unary logical negation. See perldoc perlop.

Xho
 
D

Dr.Ruud

KaZ schreef:
I'm trying to find all elements of an array that match a particular
pattern. So far, I used:

grep { /\t$var\t/ } @array

ITYM: grep /\t$var\t/, @array

Do these array-elements have <tab><some-text><tab> inside?


Variants:

grep /$var/, @array

grep /\s$var\s/, @array

grep /\b$var\b/, @array
 
A

A. Sinan Unur

KaZ schreef:


ITYM: grep /\t$var\t/, @array

C:\DOCUME~1\asu1\LOCALS~1\Temp> perldoc -f grep
grep BLOCK LIST
grep EXPR,LIST

Why do you prefer the second the first form?
grep /$var/, @array

grep /\s$var\s/, @array

grep /\b$var\b/, @array

Since you don't assign the results of grep to anything, they will be
lost.

Sinan

--
A. Sinan Unur <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
 
K

KaZ

Thanks for your help.

I didn't wrote the whole command line, since for me the problem was in
the regex. But I found the solution, it had nothing to do with the
regex.

Sorry for the stupid question about the "!", I don't know how I missed
that.
 
D

Dr.Ruud

A. Sinan Unur schreef:
Dr.Ruud:

C:\DOCUME~1\asu1\LOCALS~1\Temp> perldoc -f grep
grep BLOCK LIST
grep EXPR,LIST

Why do you prefer the second the first form?

Because the block isn't necessary. Because /\t$var\t/ is an expression.

Since you don't assign the results of grep to anything, they will be
lost.

As (I assumed that) the OP meant, I didn't care about the surroundings
like (for example) "@newarray = " and " ;".

The same format of question is:

<example>
I'm trying to find the first character at the end of a string that has a
particular value. So far, I used:

index $str, "\t", -4

but it tends to find an earlier "\t".
</example>
 
A

A. Sinan Unur

A. Sinan Unur schreef:

Because the block isn't necessary.

It is not wrong, though.
Because /\t$var\t/ is an expression.

And a block contains zero or more expressions. In the case of a single match
as above, the block form is equivalent to the expression form. There was no
need for your correction.

Sinan
 
B

Ben Morrow

Quoth "A. Sinan Unur said:
It is not wrong, though.

It is slower, though. A BLOCK has a finite amount of bookkeeping:

~% perl -MBenchmark=cmpthese -e'
my @foo = ("foo") x 1000;
cmpthese 0, {
block => sub { grep { /foo/ } @foo },
expr => sub { grep /foo/, @foo },
};'
Rate block expr
block 378/s -- -70%
expr 1279/s 238% --

It's always cleaner and often faster to say what you mean.

Ben
 
M

Mirco Wahab

Thus spoke Ben Morrow (on 2006-06-27 22:34):
It is slower, though. A BLOCK has a finite amount of bookkeeping:

~% perl -MBenchmark=cmpthese -e'
...
It's always cleaner and often faster to say what you mean.

Uhhh!

This is a very interesting (for me, at least)
aspect of array processing - one I wasn't
aware of.

I didn't expect expression-grep to come out fastest
at almost all circumstances.


Thanks for this hint!

Mirco

==>


use Benchmark qw(cmpthese);

my @foo = ("foo") x 4000;
cmpthese 0, {
iteration => sub { my @bar; for(@foo){ push(bar,$_) if /foo/ } },

expr_map => sub { my @bar = map /foo/ ? $_ : (), @foo },
expr_grep => sub { my @bar = grep /foo/, @foo },

blck_map => sub { my @bar = map { /foo/ ? $_ : () } @foo },
blck_grep => sub { my @bar = grep { /foo/ } @foo },
};
 

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

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top