Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re3: Optimizing a string processing sub

by dragonchild (Archbishop)
on Jan 08, 2003 at 21:00 UTC ( [id://225353]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Optimizing a string processing sub
in thread Optimizing a string processing sub

The @{[]} is a trick to force list context. The reason why list context is important is because of what the regex operator returns. In list context, the regex operator returns a list of what is matched. In scalar context, the regex operator returns whether or not it matched. (In theory, that should be the number of things matched as well, but I couldn't get it to work.)

So, by forcing list context, I get the list of things matched. Then, by converting the list to scalar context, I get the number of things in the list.

I'm sure there's a more elegant way than creating two array references on the fly, but that's what I had in 60sec of imperfect memory.

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Replies are listed 'Best First'.
Re: Re3: Optimizing a string processing sub
by sauoq (Abbot) on Jan 08, 2003 at 21:27 UTC
    The @{[]} is a trick to force list context.

    Actually, it does a bit more than that. It creates an array. You could have forced list context simply by using parens but a list in scalar context would return the last element in the list rather than the number of elements. So, you did what you had to. :-)

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Re3: Optimizing a string processing sub
by chromatic (Archbishop) on Jan 08, 2003 at 22:02 UTC

    chromatic's context chaining:

    my $count = () = $x =~ /(foo)/g;

    Update: I'm not claiming to have originated it; I just liked the alliteration.

Re: Re3: Optimizing a string processing sub
by MarkM (Curate) on Jan 08, 2003 at 22:04 UTC

    To ensure that characters such as '^', '-' or ']' do not interfere, I would always be tempted to surround the interpolated value with \Q..\E.

    An alternative (more intuitive?) method of forcing list context on an expression, is to assign the expression to a list:

    my $count = () = $x =~ /[\Q$y\E]/g;

    The comma operator in scalar context returns the second argument. The intermediate assignment above causes the comma operator to believe it is an list context.

    UPDATE: chromatic beat me to the draw by 2 minutes on the ()= trick. However -- chromatic: you cannot claim credit for being the first to use this trick (chromatic's context chaining). I remember it from the time that it was being argued over on the perl5-porters mailing list, perhaps 5 years ago... :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://225353]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-04-25 10:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found