Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Re: Optimizing a string processing sub

by spurperl (Priest)
on Jan 08, 2003 at 20:49 UTC ( [id://225349]=note: print w/replies, xml ) Need Help??


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

Many thanks, dragonchild, ++.

Your code works correctly and faster.
Could you please explain how exactly it works ? (the @{[ part)
  • Comment on Re: Re: Optimizing a string processing sub

Replies are listed 'Best First'.
Re3: Optimizing a string processing sub
by dragonchild (Archbishop) on Jan 08, 2003 at 21:00 UTC
    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.

      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.";
      

      chromatic's context chaining:

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

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

      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://225349]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-19 14:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found