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

Re: Multiple / Mapping Search and Replace

by jwkrahn (Abbot)
on Mar 17, 2009 at 22:19 UTC ( [id://751295]=note: print w/replies, xml ) Need Help??


in reply to Multiple / Mapping Search and Replace

You should change:

chomp $_; # Split the line by whitespace my @array = split (/\s+/, $_); # If the first column of a line is blank whitespace, shift + it out so the # first column corresponds to actual user viewable data, w +hich is the way # a user will count the columns, ignoring leading whitespa +ce. if ($array[0] eq "") { shift (@array) }

To:

# Split the line by whitespace my @array = split;

Because split with no arguments ignores leading whitespace so there is nothing to remove and it also removes all trailing whitespace so there is nothing for chomp to remove.


while (<@array>) {

That is short for:

while (glob join $", @array) {

and it is usually written as:

foreach (@array) {

Replies are listed 'Best First'.
Re^2: Multiple / Mapping Search and Replace
by VinsWorldcom (Prior) on Mar 18, 2009 at 00:48 UTC

    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])
      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];

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-19 06:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found