Concatenating regular exprs in a 'grep' call

F

freesoft12

Hi,

I want to concatenate multiple regular expressions using '||' or '&&'
operations in a 'grep' call.
---------------------
For example:

my @orig_list = ('apple', 'orange', 'pineapple','banana','pear');

# I want to find 'apple' or 'banana' in the list
my @regex_str = "/apple/ || /banana/";

# negate the concatenated expresson so that
# the 'grep' call will return 'orange' , 'pear'
my @results = grep( ! $regex_str,@orig_list);

# print 'orange', 'pear'
foreach my $r (@results) {
print $r." ";
}
print "\n";
-------------------------------------

This program currently doesn't match anything. I get an empty
'@results' list.

Any suggestions?

Regards
John
 
D

Dr.Ruud

my @orig_list = ('apple', 'orange', 'pineapple','banana','pear');

my @all = qw/ apple orange pineapple banana pear /;

# I want to find 'apple' or 'banana' in the list
my @regex_str = "/apple/ || /banana/";

You make an array where you need a scalar. Further you put a code
construct in a string (so not a Perl regex).


my @ignore = qw/ apple banana /;

my $re = join "|", @ignore;
$re = qr/\b$re\b/; # optional: add word boundaries, compile

# negate the concatenated expresson so that
# the 'grep' call will return 'orange' , 'pear'
my @results = grep( ! $regex_str,@orig_list);

my @results = grep !/$re/, @all;

# print 'orange', 'pear'
foreach my $r (@results) {
print $r." ";
}
print "\n";

print "@results\n";


So the script becomes:

#!/usr/bin/perl
use strict;
use warnings;

my @all = qw/ apple orange pineapple banana pear /;

my @ignore = qw/ apple banana /;

my $re = join "|", @ignore;
$re = qr/\b(?:$re)\b/; # optional

my @results = grep !/$re/, @all;

print "@results\n";

__END__


Because of the word boundaries, this will print pineapple too.


Alternative way to make $re:

my $re = do {local $"='|'; qr/\b(?:mad:ignore)\b/};
 
F

freesoft12

Thanks to all for your suggestions! I will read the posting guidelines
and post accordingly.

Regards
John
 
C

Charlton Wilbur

TJM> Understand the difference between what is "code" and what
TJM> is "data".

I'm tempted to ask you to elaborate on this one.

Charlton
 
U

Uri Guttman

TJM> Understand the difference between what is "code" and what
TJM> is "data".

CW> I'm tempted to ask you to elaborate on this one.

it's simple. either can be either one depending on which either you
want.

uri
 
T

Tad J McClellan

Charlton Wilbur said:
TJM> Understand the difference between what is "code" and what
TJM> is "data".

I'm tempted to ask you to elaborate on this one.


This can be hard to answer in a 3-minute post to Usenet...

The simplest example that I can come up with is:

$num = '2 + 3';

If you expect $num to contain the number five, then you have
mistaken "data" for "code".

What prompted me to say this is because the OP was treating
"data" as if it was "code":

my @regex_str = "/apple/ || /banana/"; # strings are "data"
...
my @results = grep( ! $regex_str,@orig_list);

grep's 1st arg there is simply a string (data), it is not two
pattern matches connected with the "or" operator (code).


It can be hard to discern the difference (hence Uri's either/either/either
followup):

my $str = "interpolate $variable here";

is kinda sorta both "code" (interpolation) and "data" (the resulting string)...


"eval EXPR" allows the programmer to treat "data" as if it was "code".
ie. it converts what is normally "data" into "code".

It is even muddier since eval's arg is code/data at various different points.

my $num = '3';
my $sum = eval '2 + ' . $num;

The EXPR starts out as "code". The concatenation operator with 2 operands.

After EXPR is evaluated by perl, it is "data", ie the string '2 + 3';

After eval() executes, its value is "data" (the number five) that is
the result of evaluating "code".


Some examples of posts to this newsgroup where not recognizing the
distinction between "code" and "data" led to problems:

http://groups.google.com/groups/[email protected]
(note that the MJD sub-thread is a (sarcastic) joke, not real advice)

http://groups.google.com/groups/[email protected]

http://groups.google.com/groups/[email protected]
(wherein the OP's future posts become forever invisible...)

http://groups.google.com/groups/[email protected]

http://groups.google.com/groups/[email protected]
 
C

Charlton Wilbur

TJM> Understand the difference between what is "code" and what is
TJM> "data".

TJM> This can be hard to answer in a 3-minute post to Usenet...

Indeed, at least while we're all using Von Neumann machines.

Charlton
 
L

Larry Gates

TJM> Understand the difference between what is "code" and what is
TJM> "data".


TJM> This can be hard to answer in a 3-minute post to Usenet...

Indeed, at least while we're all using Von Neumann machines.

Charlton

I think this program blurs this line:

my $filename = 'faulk15.pl';
open(my $fh, '<', $filename) or die "cannot open $filename: $!";
while (<$fh>) {
print $_;
}
close($fh)

# perl faulk15.pl

C:\MinGW\source>perl faulk15.pl
my $filename = 'faulk15.pl';
open(my $fh, '<', $filename) or die "cannot open $filename: $!";
while (<$fh>) {
print $_;
}
close($fh)

# perl faulk15.pl

--
larry gates

The human psyche is a mishmash of rules of thumb, and Einstein's thumb
is only two of them.
-- Larry Wall in <[email protected]>
 
T

Tad J McClellan

I think this program blurs this line:

my $filename = 'faulk15.pl';
open(my $fh, '<', $filename) or die "cannot open $filename: $!";
while (<$fh>) {
print $_;
}
close($fh)

# perl faulk15.pl

C:\MinGW\source>perl faulk15.pl
my $filename = 'faulk15.pl';
open(my $fh, '<', $filename) or die "cannot open $filename: $!";
while (<$fh>) {
print $_;
}
close($fh)

# perl faulk15.pl


http://en.wikipedia.org/wiki/Quine_(computing)
http://www.nyx.net/~gthompso/quine.htm


Some quines in Perl:

http://www.nyx.net/~gthompso/self_perl.txt
 

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,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top