Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Interesting line which I read from Monk's snippets

by gryphon (Abbot)
on Jan 31, 2006 at 05:32 UTC ( #526666=note: print w/ replies, xml ) Need Help??


in reply to Interesting line which I read from Monk's snippets

Greetings jesuashok,

It's a very curious line, and others have explained what it does already; however, it has a couple negatives: It's not easily understood, and it's slower than a more easily understood alternative.

use strict; use warnings; use Benchmark qw(cmpthese); cmpthese( 100000, { 'ahash' => sub { my $pat = {qw(a - b = c ~ d ^)}->{substr($0, 0, 1)}; }, 'tr' => sub { (my $pat = substr($0, 0, 1)) =~ tr/abcd/-=~^/; }, }, );

The above benchmark returns this:

Rate ahash tr ahash 75120/s -- -86% tr 541712/s 621% --

It's a nice trick, but I wouldn't use it anywhere except in an obfu.

gryphon
Whitepages.com Development Manager (WDDC)
code('Perl') || die;


Comment on Re: Interesting line which I read from Monk's snippets
Select or Download Code
Re^2: Interesting line which I read from Monk's snippets
by pKai (Priest) on Jan 31, 2006 at 08:30 UTC

    To deliver the same result (i. e. undef) for not found input values, the tr solution has to be a bit more elaborate.

    Somewhat along the line of

    my $found = (my $pat = substr($0,0,1)) =~ tr/abcd/-=~^/; $pat = $found ? $pat : undef;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (13)
As of 2014-10-31 09:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (216 votes), past polls