[28028] in Perl-Users-Digest
Perl-Users Digest, Issue: 9392 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jun 27 14:10:15 2006
Date: Tue, 27 Jun 2006 11:10:09 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Perl-Users Digest Tue, 27 Jun 2006 Volume: 10 Number: 9392
Today's topics:
Re: Regular Expression Generator <tzz@lifelogs.com>
Searching each element of an array with grep <kaz219@gawab.com>
Re: Searching each element of an array with grep xhoster@gmail.com
Re: Searching each element of an array with grep <rvtol+news@isolution.nl>
Re: Searching each element of an array with grep <jgibson@mail.arc.nasa.gov>
Re: Searching each element of an array with grep <1usa@llenroc.ude.invalid>
Re: Searching each element of an array with grep <kaz219@gawab.com>
Re: Searching each element of an array with grep <rvtol+news@isolution.nl>
Re: Test loop <tzz@lifelogs.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 27 Jun 2006 13:50:29 -0400
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: Regular Expression Generator
Message-Id: <g69y7vi8p3u.fsf@CN1374059D0130.kendall.corp.akamai.com>
On 26 Jun 2006, noreply@invalid.net wrote:
Dr.Ruud wrote:
>
>> Ted Zlatanov schreef:
>>
>>> my $regex = '^(' . join('|', @strings) . ')$';
>>>
>>> and that's a regex that will match any given non-empty strings.
>>
>> '^(?:' . join( '|', map quotemeta, grep /./, @strings ) . ')$'
>
> This solution has a caveat. Regexps have a maximum length (65539 bytes I
> believe). If you have enough strings in @strings (or if they are long
> enough), then the compiled regexp can exceed this length, and error out. I
> encountered this once, and the solution I resorted to was to construct an
> anonymous sub on the fly:
You and Dr. Ruud make great points. My original code was written in
haste, sorry about that. If I did it with some brainwaves active, it
would have been:
# untested
my %hash;
$hash{$_} = 1 foreach @strings;
sub matches { return exists $hash{shift()};}
No need for subroutines and eval(). Then you can use matches() in the
regex as a code escape :) Isn't Perl great?
Ted
------------------------------
Date: 27 Jun 2006 09:08:58 -0700
From: "KaZ" <kaz219@gawab.com>
Subject: Searching each element of an array with grep
Message-Id: <1151424538.934504.212860@i40g2000cwc.googlegroups.com>
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,
------------------------------
Date: 27 Jun 2006 16:26:01 GMT
From: xhoster@gmail.com
Subject: Re: Searching each element of an array with grep
Message-Id: <20060627122624.114$ao@newsreader.com>
"KaZ" <kaz219@gawab.com> wrote:
> 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
--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
------------------------------
Date: Tue, 27 Jun 2006 18:26:01 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: Searching each element of an array with grep
Message-Id: <e7rtjp.js.1@news.isolution.nl>
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
--
Affijn, Ruud
"Gewoon is een tijger."
------------------------------
Date: Tue, 27 Jun 2006 10:00:16 -0700
From: Jim Gibson <jgibson@mail.arc.nasa.gov>
Subject: Re: Searching each element of an array with grep
Message-Id: <270620061000163044%jgibson@mail.arc.nasa.gov>
In article <1151424538.934504.212860@i40g2000cwc.googlegroups.com>, KaZ
<kaz219@gawab.com> wrote:
> 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.
A regular expression either matches or doesn't match. We would need to
see the contents of $var and @array to tell you why any particular
element of @array either matches or doesn't.
If you want 'exact' matches between $var and any element of @array, you
can use the RE /^\Q$var$/, where \Q will quote any meta-characters in
$var. You can also use the more efficient 'eq' operator:
@matches = grep { $_ eq $var } @array;
If you want to get more help, please adhere to the guidelines for this
group and post a short, complete, working program that illustrates the
problems you are having. Thanks.
>
> Here:
> http://perldoc.perl.org/functions/grep.html
> there is a "!" before the "/". What does this means? I didn't find
> anything about this...
The ! here is the logical negation operator. The example is trying to
weed out comments:
@foo = grep { !/^#/ } @bar;
The regular expression /^#/ returns true for any line whose first
character is #. The ! flips that value to false, so that any line whose
first character is # is not included in the scalar list returned by
grep. Lines whose first character is NOT # end up in @foo.
>
> I also tried /.*\t$var\t.*/ but it doesn't match either...
There is no difference between /\t$var\t/ and /.*\t$var\t.*/ (except
for possible side effects). They will both match or both not match a
given string. .* at the beginning or end of a string is superfluous.
------------------------------
Date: Tue, 27 Jun 2006 17:06:27 GMT
From: "A. Sinan Unur" <1usa@llenroc.ude.invalid>
Subject: Re: Searching each element of an array with grep
Message-Id: <Xns97EF856EE3E99asu1cornelledu@127.0.0.1>
"Dr.Ruud" <rvtol+news@isolution.nl> wrote in news:e7rtjp.js.1
@news.isolution.nl:
> 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
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 <1usa@llenroc.ude.invalid>
(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
------------------------------
Date: 27 Jun 2006 10:08:34 -0700
From: "KaZ" <kaz219@gawab.com>
Subject: Re: Searching each element of an array with grep
Message-Id: <1151428114.174680.148590@j72g2000cwa.googlegroups.com>
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.
------------------------------
Date: Tue, 27 Jun 2006 19:49:28 +0200
From: "Dr.Ruud" <rvtol+news@isolution.nl>
Subject: Re: Searching each element of an array with grep
Message-Id: <e7s27g.vs.1@news.isolution.nl>
A. Sinan Unur schreef:
> Dr.Ruud:
>> KaZ:
>>> 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
>
> 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.
>> 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.
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>
--
Affijn, Ruud
"Gewoon is een tijger."
------------------------------
Date: Tue, 27 Jun 2006 13:55:56 -0400
From: Ted Zlatanov <tzz@lifelogs.com>
Subject: Re: Test loop
Message-Id: <g69u0668our.fsf@CN1374059D0130.kendall.corp.akamai.com>
On 26 Jun 2006, kaz219@gawab.com wrote:
> I'm trying to do the following:
> for each value of an array, make a test.
> If the loop comes to the end without that the test became true a single
> time, do action A. Otherwise, just go on, without doing action A.
my $successful_test = 0;
foreach my $value (@values)
{
$successful_test ||= run_test($value);
}
runActionA() unless $successful_test;
Various grep/map solutions are possible but this is probably easiest
to understand.
Ted
------------------------------
Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin)
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>
Administrivia:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.
------------------------------
End of Perl-Users Digest V10 Issue 9392
***************************************