Ask a Question related to PERL Miscellaneous, Design and Development.
-
Matija Papec #1
qr// question
How can one know the difference between $arr[0] and $arr[1]?
eg.
my @arr = ('^match') x 2;
$_ = qr/$_/ for $arr[0];
I wouldn't want to do $_ = qr/$_/ twice on same regex (I guess it's time
consuming?)
--
Matija
Matija Papec Guest
-
Newbie Question: Biz Card Template Question
Hi, I got the Pagemaker PlugIn - I am using one of the templates for Business Cards - the elements appear to be grouped (bound box all around when I... -
Good morning or good evening depending upon your location. I want to ask you the most important question of your life. Your joy or sorrow for all eternity depends upon your answer. The question is: Are you saved? It is not a question of how good you
<RonGrossi382872@yahoo.com> wrote in message news:1114393703.900419.199790@f14g2000cwb.googlegroups.com... This is the most important question of... -
John W. Krahn #2
Re: qr// question
Matija Papec wrote:
Use 'eq' or '==' to compare them?>
> How can one know the difference between $arr[0] and $arr[1]?
In this instance the contents of $arr[0] and $arr[1] are exactly the> eg.
> my @arr = ('^match') x 2;
same.
What do you REALLY want to do?> $_ = qr/$_/ for $arr[0];
>
> I wouldn't want to do $_ = qr/$_/ twice on same regex (I guess it's time
> consuming?)
John
--
use Perl;
program
fulfillment
John W. Krahn Guest
-
Matija Papec #3
Re: qr// question
On Mon, 08 Sep 2003 21:19:19 GMT, "John W. Krahn" <krahnj@acm.org>
wrote:
Yes, but after below line they /somehow/ aren't.>>> my @arr = ('^match') x 2;
>In this instance the contents of $arr[0] and $arr[1] are exactly the
>same.
I want hash/array of regexes but don't want to instantly compile them>>> $_ = qr/$_/ for $arr[0];
>>
>> I wouldn't want to do $_ = qr/$_/ twice on same regex (I guess it's time
>> consuming?)
>What do you REALLY want to do?
all. I would like to compile them only when some particular is needed
and that "some particular" may be used more then once for matching,
while some regexes may not be used at all. If regex compiling is
significantly time consuming then this matters.
Matija Papec Guest
-
Sam Holden #4
Re: qr// question
On Tue, 09 Sep 2003 08:30:57 +0200, Matija Papec <mpapec@yahoo.com> wrote:
Something like:> On Mon, 08 Sep 2003 21:19:19 GMT, "John W. Krahn" <krahnj@acm.org>
> wrote:
>>>>>>> my @arr = ('^match') x 2;
>>In this instance the contents of $arr[0] and $arr[1] are exactly the
>>same.
> Yes, but after below line they /somehow/ aren't.
>>>>>>> $_ = qr/$_/ for $arr[0];
>>>
>>> I wouldn't want to do $_ = qr/$_/ twice on same regex (I guess it's time
>>> consuming?)
>>What do you REALLY want to do?
> I want hash/array of regexes but don't want to instantly compile them
> all. I would like to compile them only when some particular is needed
> and that "some particular" may be used more then once for matching,
> while some regexes may not be used at all. If regex compiling is
> significantly time consuming then this matters.
use Memoize;
memoize('to_regex');
sub to_regex {
return qr/$_[0]/;
}
or
my %regexes;
sub to_regex {
$regexes{$_[0]} = qr/$_[0]/ unless exists $regexes{$_[0]};
return $regexes{$_[0]};
}
And then when you want to match use
$re = to_regex($string_of_regex);
to get the regex for that string.
Or with an array (or hash with tr/[]/{}/ ) of regexes you could do:
$arr[$index] = qr/$arr[$index]/ unless ref $arr[$index];
Before using $arr[$index] as a regex.
--
Sam Holden
Sam Holden Guest
-
Anno Siegel #5
Re: qr// question
Matija Papec <mpapec@yahoo.com> wrote in comp.lang.perl.misc:
Access the regexes through this routine:> On Mon, 08 Sep 2003 21:19:19 GMT, "John W. Krahn" <krahnj@acm.org>
> wrote:
>>> >> >> my @arr = ('^match') x 2;
> >In this instance the contents of $arr[0] and $arr[1] are exactly the
> >same.
> Yes, but after below line they /somehow/ aren't.
>>> >> >> $_ = qr/$_/ for $arr[0];
> >>
> >> I wouldn't want to do $_ = qr/$_/ twice on same regex (I guess it's time
> >> consuming?)
> >What do you REALLY want to do?
> I want hash/array of regexes but don't want to instantly compile them
> all. I would like to compile them only when some particular is needed
> and that "some particular" may be used more then once for matching,
> while some regexes may not be used at all. If regex compiling is
> significantly time consuming then this matters.
sub to_regex {
ref $_[ 0] ? $_[ 0] : $_[ 0] = qr/$_[0]/;
}
Strings will successively be turned into regexes on first use.
Anno
Anno Siegel Guest
-
Brian McCauley #6
Re: qr// question
[email]anno4000@lublin.zrz.tu-berlin.de[/email] (Anno Siegel) writes:
And if it isn't then it doesn't.> Matija Papec <mpapec@yahoo.com> wrote in comp.lang.perl.misc:> > On Mon, 08 Sep 2003 21:19:19 GMT, "John W. Krahn" <krahnj@acm.org>
> > wrote:
> >> >> > >> my @arr = ('^match') x 2;
> > >
> > >In this instance the contents of $arr[0] and $arr[1] are exactly the
> > >same.
> > Yes, but after below line they /somehow/ aren't.
> >> >> > >> $_ = qr/$_/ for $arr[0];
> > >>
> > >> I wouldn't want to do $_ = qr/$_/ twice on same regex (I guess it's time
> > >> consuming?)
> > >
> > >What do you REALLY want to do?
> > I want hash/array of regexes but don't want to instantly compile them
> > all. I would like to compile them only when some particular is needed
> > and that "some particular" may be used more then once for matching,
> > while some regexes may not be used at all. If regex compiling is
> > significantly time consuming then this matters.
The text and the subroutine invocation are probably more expensive than the> Access the regexes through this routine:
>
> sub to_regex {
> ref $_[ 0] ? $_[ 0] : $_[ 0] = qr/$_[0]/;
> }
qr//.
#!/usr/bin/perl
use strict;
use warnings;
use Benchmark;
sub to_regex {
ref $_[ 0] ? $_[ 0] : $_[ 0] = qr/$_[0]/;
}
timethese 10 => {
to_regex => sub {
my $q = q/x..........[yz]+[[:upper:]]*z(?=r)/;
$q = to_regex $q for 0..100000;
},
qr => sub {
my $q = q/x..........[yz]+[[:upper:]]*z(?=r)/;
$q = qr/$q/ for 0..100000;
},
};
__END__
Benchmark: timing 10 iterations of qr, to_regex...
qr: 4 wallclock secs ( 3.52 usr + 0.00 sys = 3.52 CPU) @ 2.84/s (n=10)
to_regex: 6 wallclock secs ( 6.91 usr + 0.00 sys = 6.91 CPU) @ 1.45/s (n=10)
--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
Brian McCauley Guest
-
Anno Siegel #7
Re: qr// question
Brian McCauley <nobull@mail.com> wrote in comp.lang.perl.misc:
Regex compilation *is* rather expensive. The real reason is that /$re/> [email]anno4000@lublin.zrz.tu-berlin.de[/email] (Anno Siegel) writes:
>>> > Matija Papec <mpapec@yahoo.com> wrote in comp.lang.perl.misc:> > > On Mon, 08 Sep 2003 21:19:19 GMT, "John W. Krahn" <krahnj@acm.org>
> > > wrote:
> > >
> > > >> my @arr = ('^match') x 2;
> > > >
> > > >In this instance the contents of $arr[0] and $arr[1] are exactly the
> > > >same.
> > >
> > > Yes, but after below line they /somehow/ aren't.
> > >
> > > >> $_ = qr/$_/ for $arr[0];
> > > >>
> > > >> I wouldn't want to do $_ = qr/$_/ twice on same regex (I guess it's time
> > > >> consuming?)
> > > >
> > > >What do you REALLY want to do?
> > >
> > > I want hash/array of regexes but don't want to instantly compile them
> > > all. I would like to compile them only when some particular is needed
> > > and that "some particular" may be used more then once for matching,
> > > while some regexes may not be used at all. If regex compiling is
> > > significantly time consuming then this matters.
> And if it isn't then it doesn't.
>>> > Access the regexes through this routine:
> >
> > sub to_regex {
> > ref $_[ 0] ? $_[ 0] : $_[ 0] = qr/$_[0]/;
> > }
> The text and the subroutine invocation are probably more expensive than the
> qr//.
doesn't compile the regex again when $re is a single qr//-quoted value.
That would explain your benchmarks. It also makes this a rather futile
exercise.
[snip benchmark]
Anno
Anno Siegel Guest
-
Jeff 'japhy' Pinyan #8
Re: qr// question
On 9 Sep 2003, Anno Siegel wrote:
.... and every time after that. = binds more loosely than ?:>Access the regexes through this routine:
>
> sub to_regex {
> ref $_[ 0] ? $_[ 0] : $_[ 0] = qr/$_[0]/;
> }
>
>Strings will successively be turned into regexes on first use.
perlmonk:~ $ perl -MO=Deparse,-p -e 'ref $a ? $a : $a = $b'
((ref($a) ? $a : $a) = $b);
It's the same trap as $cond ? $a = $b : $a = $c, which some people use
instead of $a = ($cond ? $b : $c), but always sets $a to $c.
--
Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
"And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
Jeff 'japhy' Pinyan Guest
-
Matija Papec #9
Re: qr// question
X-Ftn-To: Anno Siegel
[email]anno4000@lublin.zrz.tu-berlin.de[/email] (Anno Siegel) wrote:Tnx, so ref knows all about them! :) Unfortunately perldoc isn't so>>> and that "some particular" may be used more then once for matching,
>> while some regexes may not be used at all. If regex compiling is
>> significantly time consuming then this matters.
>Access the regexes through this routine:
>
> sub to_regex {
> ref $_[ 0] ? $_[ 0] : $_[ 0] = qr/$_[0]/;
> }
>
>Strings will successively be turned into regexes on first use.
informative. :!
--
Matija
Matija Papec Guest
-
Anno Siegel #10
Re: qr// question
Jeff 'japhy' Pinyan <pinyaj@rpi.edu> wrote in comp.lang.perl.misc:
Oh, right. Thanks for catching that. Parentheses around the assignment> On 9 Sep 2003, Anno Siegel wrote:
>>> >Access the regexes through this routine:
> >
> > sub to_regex {
> > ref $_[ 0] ? $_[ 0] : $_[ 0] = qr/$_[0]/;
> > }
> >
> >Strings will successively be turned into regexes on first use.
> ... and every time after that. = binds more loosely than ?:
would fix it, but, as noted in another branch of the thread, it doesn't
save on regex compilation either way: qr// (well, the regex compiler itself)
already does that.
Anno
Anno Siegel Guest
-
Anno Siegel #11
Re: qr// question
Matija Papec <mpapec@yahoo.com> wrote in comp.lang.perl.misc:
You can think of qr/.../ values as objects of class Regexp, which has> X-Ftn-To: Anno Siegel
>
> [email]anno4000@lublin.zrz.tu-berlin.de[/email] (Anno Siegel) wrote:>> >> >> and that "some particular" may be used more then once for matching,
> >> while some regexes may not be used at all. If regex compiling is
> >> significantly time consuming then this matters.
> >Access the regexes through this routine:
> >
> > sub to_regex {
> > ref $_[ 0] ? $_[ 0] : $_[ 0] = qr/$_[0]/;
> > }
> >
> >Strings will successively be turned into regexes on first use.
> Tnx, so ref knows all about them! :) Unfortunately perldoc isn't so
> informative. :!
stringification overloaded. That describes most of their behavior, though
things aren't really that simple.
perl -Moverload -le'print "yup" if overload::Overloaded( qr//)'
prints nothing.
Anno
Anno Siegel Guest
-
Matija Papec #12
Re: qr// question
X-Ftn-To: Sam Holden
[email]sholden@flexal.cs.usyd.edu.au[/email] (Sam Holden) wrote:Tnx, last one looks most promising as it doesn't use any additional>my %regexes;
>sub to_regex {
> $regexes{$_[0]} = qr/$_[0]/ unless exists $regexes{$_[0]};
> return $regexes{$_[0]};
>}
>
>And then when you want to match use
>
>$re = to_regex($string_of_regex);
>
>to get the regex for that string.
>
>Or with an array (or hash with tr/[]/{}/ ) of regexes you could do:
>
>$arr[$index] = qr/$arr[$index]/ unless ref $arr[$index];
>
>Before using $arr[$index] as a regex.
variables.
--
Matija
Matija Papec Guest
-
Anno Siegel #13
Re: qr// question
Matija Papec <mpapec@yahoo.com> wrote in comp.lang.perl.misc:
[qr// values]> [email]anno4000@lublin.zrz.tu-berlin.de[/email] (Anno Siegel) wrote:
"Perl does what you want, unless you want consistency">> >That describes most of their behavior, though
> >things aren't really that simple.
> >
> > perl -Moverload -le'print "yup" if overload::Overloaded( qr//)'
> >
> >prints nothing.
> Obviously consistency is hard thing to achieve.
Anno
Anno Siegel Guest
-
John W. Krahn #14
Re: qr// question
Anno Siegel wrote:
Can I quote you on that? :-)>
> Matija Papec <mpapec@yahoo.com> wrote in comp.lang.perl.misc:>> >
> > Obviously consistency is hard thing to achieve.
> "Perl does what you want, unless you want consistency"
John
--
use Perl;
program
fulfillment
John W. Krahn Guest
-
Anno Siegel #15
Re: qr// question
John W. Krahn <krahnj@acm.org> wrote in comp.lang.perl.misc:
You'd be quoting Larry Wall, more or less, or at least some edition of> Anno Siegel wrote:>> >
> > Matija Papec <mpapec@yahoo.com> wrote in comp.lang.perl.misc:> >> > >
> > > Obviously consistency is hard thing to achieve.
> > "Perl does what you want, unless you want consistency"
> Can I quote you on that? :-)
the Camel. It wasn't about Perl in general, but, I think it assignment
in various contexts that he said that.
Anno
Anno Siegel Guest



Reply With Quote

