Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: Multiple / Mapping Search and Replace

by VinsWorldcom (Prior)
on Mar 18, 2009 at 00:48 UTC ( [id://751325]=note: print w/replies, xml ) Need Help??


in reply to Re: Multiple / Mapping Search and Replace
in thread Multiple / Mapping Search and Replace

Thanks for the tips. Incorporated and tested fine.

Question: What is better coding practice and quicker?

if (defined($opt_ignore)) { if (defined($opt_reverse)) { $map{lc($array[1])} = $array[0] } else { $map{lc($array[0])} = $array[1] } } else { if (defined($opt_reverse)) { $map{$array[1]} = $array[0] } else { $map{$array[0]} = $array[1] } }

or

$map{ (defined($opt_ignore) ? lc( (defined($opt_reverse) ? $array[1] : + $array[0])) : (defined($opt_reverse) ? $array[1] : $array[0])) } = ( +defined($opt_reverse) ? $array[0] : $array[1])

Replies are listed 'Best First'.
Re^3: Multiple / Mapping Search and Replace
by graff (Chancellor) on Mar 18, 2009 at 05:22 UTC
    I would avoid both of those approaches. The first involves too much repetition of code, and the second is too obfuscated. How about like this:
    my ( $src, $dst ) = ( $opt_reverse ) ? ( 0, 1 ) : ( 1, 0 ); if ( $opt_ignore ) { $map{lc($array[$dst])} = $array[$src]; } else { $map{$array[$dst]} = $array[$src]; }
    (I hope I got the polarity right on what "reverse" means, but in any case, I think you can see the point.)

    (update: got rid of the "defined()" call -- the option values should just be true or false, and undefined is false)

    Another update: better solution:

    my ( $src, $dst ) = ( $opt_reverse ) ? (0,1) : (1,0); my $key = ( $opt_ignore ) ? lc($array[$dst]) : $array[$dst]; $map{$key} = $array[$src];
Re^3: Multiple / Mapping Search and Replace
by jwkrahn (Abbot) on Mar 18, 2009 at 01:23 UTC

    The first one looks a lot more readable and maintainable.   As for speed, you would have to use something like Benchmark to determine which is fastest.

Log In?
Username:
Password:

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

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

    No recent polls found