[30974] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 2219 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Feb 18 14:14:24 2009

Date: Wed, 18 Feb 2009 11:14:16 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Wed, 18 Feb 2009     Volume: 11 Number: 2219

Today's topics:
        regexp or array? <joblo@cretin.fr>
    Re: regexp or array? <peter@makholm.net>
    Re: regexp or array? <tadmc@seesig.invalid>
    Re: regexp or array? <tadmc@seesig.invalid>
    Re: regexp or array? <perl@marc-s.de>
    Re: regexp or array? <joblo@cretin.fr>
    Re: regexp or array? <tadmc@seesig.invalid>
    Re: regexp or array? <glex_no-spam@qwest-spam-no.invalid>
    Re: regexp or array? <perl@marc-s.de>
    Re: regexp or array? sln@netherlands.com
    Re: Undefined subroutine &main::param called <zen13097@zen.co.uk>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: 18 Feb 2009 11:35:25 GMT
From: End of Road <joblo@cretin.fr>
Subject: regexp or array?
Message-Id: <499bf27d$0$4075$ba4acef3@news.orange.fr>

This is a question about efficiency. Say I have to check a string against 
a series of strings: to check, that is, if the string has a match or not.
I have two ways of doing this.
Using regular expressions

$strs = 'str1|str2|str3";
if ($str =~ /strs/)
  ...

Or using an array

@strs = qw/str1,str2, str3/;
foreach (@strs) {
   if ($_[0] =~ /$_/)
       ...

these are equivalent methods; but which one is faster?



------------------------------

Date: Wed, 18 Feb 2009 13:38:33 +0100
From: Peter Makholm <peter@makholm.net>
Subject: Re: regexp or array?
Message-Id: <87fxibapiu.fsf@vps1.hacking.dk>

End of Road <joblo@cretin.fr> writes:

> Using regular expressions
>
> $strs = 'str1|str2|str3";
> if ($str =~ /strs/)
>   ...
>
> Or using an array
>
> @strs = qw/str1,str2, str3/;
> foreach (@strs) {
>    if ($_[0] =~ /$_/)
>        ...

Better use an array of precompiled regexpes:

my @regexp = map { qr/$_/ } qw(foo bar baz);

if( grep { $_[0] =~ $_ } @regexp ) {

  ...

}

or even better

use List::MoreUtils qw(any);

my @regexp = map { qr/$_/ } qw(foo bar baz);
if ( any { $_[0] =~ $_ } @regexp ) { 
  ...
}


> these are equivalent methods; but which one is faster?

The Benchmark-module can tell you that. Remember to use a realistic
input.

//Makholm


------------------------------

Date: Wed, 18 Feb 2009 06:27:21 -0600
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: regexp or array?
Message-Id: <slrngpnvl9.c09.tadmc@tadmc30.sbcglobal.net>

End of Road <joblo@cretin.fr> wrote:

> $strs = 'str1|str2|str3";
> if ($str =~ /strs/)
>   ...
>
> Or using an array
>
> @strs = qw/str1,str2, str3/;
> foreach (@strs) {
>    if ($_[0] =~ /$_/)
>        ...
>
> these are equivalent methods


No they're not.


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


------------------------------

Date: Wed, 18 Feb 2009 06:26:40 -0600
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: regexp or array?
Message-Id: <slrngpnvk0.c09.tadmc@tadmc30.sbcglobal.net>

End of Road <joblo@cretin.fr> wrote:

> @strs = qw/str1,str2, str3/;


You should always enable warnings when developing Perl code!


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


------------------------------

Date: Wed, 18 Feb 2009 14:09:12 +0100
From: Marc Lucksch <perl@marc-s.de>
Subject: Re: regexp or array?
Message-Id: <gnh18s$1hd1$1@ariadne.rz.tu-clausthal.de>

End of Road schrieb:
> This is a question about efficiency. Say I have to check a string against 
> a series of strings: to check, that is, if the string has a match or not.
> I have two ways of doing this.
> Using regular expressions


> $strs = 'str1|str2|str3";
> if ($str =~ /strs/)
>   ...
> 
> Or using an array
> 
> @strs = qw/str1,str2, str3/;
> foreach (@strs) {
>    if ($_[0] =~ /$_/)
>        ...
More like
@strs = qw/str1 str2 str3/;
  foreach (@strs) {
     if ($str eq /$_/)
         ...

The best would be in my opinion:

my %lookup=(
	str1=>1,
	str2=>1,
	str3=1,
	#.....
);

if ($lookup{$str}) {
	....
}

Marc "Maluku" Lucksch


------------------------------

Date: 18 Feb 2009 14:39:58 GMT
From: End of Road <joblo@cretin.fr>
Subject: Re: regexp or array?
Message-Id: <499c1dbd$0$17774$ba4acef3@news.orange.fr>

On Wed, 18 Feb 2009 06:27:21 -0600, Tad J McClellan wrote:

> End of Road <joblo@cretin.fr> wrote:
> 
>> $strs = 'str1|str2|str3";
>> if ($str =~ /strs/)
>>   ...
>>
>> Or using an array
>>
>> @strs = qw/str1,str2, str3/;
>> foreach (@strs) {
>>    if ($_[0] =~ /$_/)
>>        ...
>>
>> these are equivalent methods
> 
> 
> No they're not.

what I meant by equivalent is that both methods are different ways of 
achieving the same thing, am I wrong?


------------------------------

Date: Wed, 18 Feb 2009 09:12:49 -0600
From: Tad J McClellan <tadmc@seesig.invalid>
Subject: Re: regexp or array?
Message-Id: <slrngpo9bh.drf.tadmc@tadmc30.sbcglobal.net>

End of Road <joblo@cretin.fr> wrote:
> On Wed, 18 Feb 2009 06:27:21 -0600, Tad J McClellan wrote:
>
>> End of Road <joblo@cretin.fr> wrote:
>> 
>>> $strs = 'str1|str2|str3";
            ^              ^
            ^              ^
>>> if ($str =~ /strs/)
                 ^
                 ^ where is the dollar sign?
>>>   ...
>>>
>>> Or using an array
>>>
>>> @strs = qw/str1,str2, str3/;
>>> foreach (@strs) {
>>>    if ($_[0] =~ /$_/)
>>>        ...
>>>
>>> these are equivalent methods
>> 
>> 
>> No they're not.
>
> what I meant by equivalent is that both methods are different ways of 
> achieving the same thing, am I wrong?


Yes, you are wrong.

The 1st one does not even compile.

The 1st ones matches against $str, the 2nd one against $_[0].

The 1st one has a /str1/ pattern, the 2nd one has a /str1,str2,/ pattern.

Have you seen the Posting Guidelines that are posted here frequently?


Your Question is Asked Frequently:

    perldoc -q match

        How do I efficiently match many regular expressions at once?


-- 
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"


------------------------------

Date: Wed, 18 Feb 2009 10:42:14 -0600
From: "J. Gleixner" <glex_no-spam@qwest-spam-no.invalid>
Subject: Re: regexp or array?
Message-Id: <499c3a67$0$87071$815e3792@news.qwest.net>

Marc Lucksch wrote:
> End of Road schrieb:
>> This is a question about efficiency. Say I have to check a string 
>> against a series of strings: to check, that is, if the string has a 
>> match or not.
>> I have two ways of doing this.
>> Using regular expressions
> 
> 
>> $strs = 'str1|str2|str3";
>> if ($str =~ /strs/)
>>   ...
>>
>> Or using an array
>>
>> @strs = qw/str1,str2, str3/;
>> foreach (@strs) {
>>    if ($_[0] =~ /$_/)
>>        ...
> More like
> @strs = qw/str1 str2 str3/;
>  foreach (@strs) {
>     if ($str eq /$_/)
                ^^^^^^^
Hu?


------------------------------

Date: Wed, 18 Feb 2009 18:02:22 +0100
From: Marc Lucksch <perl@marc-s.de>
Subject: Re: regexp or array?
Message-Id: <gnheu1$1pt4$1@ariadne.rz.tu-clausthal.de>

J. Gleixner schrieb:
> Marc Lucksch wrote:
>> @strs = qw/str1 str2 str3/;
>>  foreach (@strs) {
>>     if ($str eq /$_/)
>                ^^^^^^^
> Hu?

Arrg, you know what I meant

if ($str eq $_) {

Marc "Maluku" Lucksch


------------------------------

Date: Wed, 18 Feb 2009 17:54:41 GMT
From: sln@netherlands.com
Subject: Re: regexp or array?
Message-Id: <04bop4hnrqvdgdb012vtc29ep7hldvi535@4ax.com>

On 18 Feb 2009 11:35:25 GMT, End of Road <joblo@cretin.fr> wrote:

>This is a question about efficiency. Say I have to check a string against 
>a series of strings: to check, that is, if the string has a match or not.
>I have two ways of doing this.
>Using regular expressions
>
>$strs = 'str1|str2|str3";
>if ($str =~ /strs/)
>  ...
>
>Or using an array
>
>@strs = qw/str1,str2, str3/;
>foreach (@strs) {
>   if ($_[0] =~ /$_/)
>       ...
>
>these are equivalent methods; but which one is faster?

SIMPLE case:
Linear search begin to end, no quantifiers or modifiers.
And assumes you are breaking out of the foreach loop when you find the first
occurance.

For->  $strs = 'str1|str2|str3";
  Context switching - (Disadvantage) In essence, on each position in $str, the context is switched in the OR, 
  adding overhead. So if you had a 20 char $str, you would have 20 x 3 = 60 switches max.
  Comparisons - (20 char $str)
        str1|str2|str3, Start  = 1-3 
        str1|str2|str3, Middle = 27-30
        str1|str2|str3, End    = 57-60

For->  looping  $_[0] =~ /$_/;
  Context switching - (Advantage) Done a max of 3 times, there is no OR.
  Comparisons - (20 char $str)
        str1, Start  = 1                   0     Neutral
        str1, Middle = 10                -20       |
        str1, End    = 20                -40     Advantage

        str2, Start  = 20+1  = 21        +20     Disadvantage
        str2, Middle = 20+10 = 30          0     Neutral
        str2, End    = 20+20 = 40        -20     Advantage

        str3, Start  = 40+1  = 41        +40     Disadvantage
        str3, Middle = 40+10 = 50        +20       |
        str3, End    = 40+20 = 60          0     Neutral
                                        ----
                               Average:    0

For the looping comparisons, you can see that for a single match,
  End's have an advantage
  Start's have a disadvantage

For the OR conditional comparisons the reverse is true,
  Start's have an advantage
  End's have a a disadvantage

Thats why the conventional logic when using OR's in regex's suggests that
you put the item that will match the most in the Start 'or' position,
and the least in the End 'or' position.

If that is the case, the OR will be faster, but if random data exists in
a vacuum is to be tested, neither the OR nor the Loop will come out the winner.

However, there is the context switching overhead with OR's. This is mitigated
when complex expressions are used where to break out into simple expressions
involving tracking of operations between expressions would be prohibitively
time/memory consuming.

-sln







------------------------------

Date: 18 Feb 2009 08:35:17 GMT
From: Dave Weaver <zen13097@zen.co.uk>
Subject: Re: Undefined subroutine &main::param called
Message-Id: <499bc845$0$16171$db0fefd9@news.zen.co.uk>

On Tue, 17 Feb 2009 23:16:25 -0800 (PST),
   paul.hopgood@deathnotify.com <paul.hopgood@deathnotify.com> wrote:
>  I need help solving a subroutine error in my Pearl script.
>  Here is the error I'm getting:
>  binmode() on closed filehandle SAVE at /recording.pl line 12.
>  Undefined subroutine &main::param called at /recording.pl line 13.
> 
>  Below is my script:
> 
>  #!/usr/bin/perl -w
> 
>  use strict;
>  $|++;
> 
>  my $pathToRecordings="http://www.deathnotify.com/recordings";
>  my $newRecording = "test.wav";

You don't refer to $newRecording anywhere in your script.


>   open (SAVE, "> $pathToRecordings/recordings");

You should *always* check the return value from open() - it'll tell
you if the open worked or failed (and why it failed). Also it's better
to use lexical filehandles and the 3-argument form of open. Like this:

    open my $save, '>', $filename
         or die "Can't write to '$filename' because $!";

You're trying to write to a file called
     "http://www.deathnotify.com/recordings/recordings"
It appears as though you're trying up write to some website.
open() reads or writes to files on the local filesystem, and can't
write to URLs.

If you are trying to send the file to a different computer, you
need to use some other method to do it. FTP, for example, using the
Net::FTP module.


>     binmode(SAVE);

You got your 1st error messge here, because the open() failed (and
you didn't notice), so SAVE doesn't refer to an open file.

>     while (read(param("voiceMessage"),$newRecording,1024))

Your script has no param() function, yet here you are trying to call
it, which is the reason for your second error message.

Were you thinking of the param() function provided by the CGI module?
Is your script running as a CGI script on a webserver? If so, perhaps
you forgot
   use CGI;
at the start of your script.


------------------------------

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 V11 Issue 2219
***************************************


home help back first fref pref prev next nref lref last post