Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: How to improve my code? main concern:array as hash element

by Util (Priest)
on Nov 23, 2011 at 16:59 UTC ( #939698=note: print w/ replies, xml ) Need Help??


in reply to How to improve my code? main concern:array as hash element

++wfsp. For uniformity, I would store *every* value of %gp as an arrayref, rather than need conditional logic on every use of a value.

Here is my quick refactor, with ++hbm's test data (padded to 5 columns):

use strict; use warnings; my %gp = ( G11 => [qw( Galpha-11 )], G12 => [qw( Galpha-12 )], G13 => [qw( Galpha-13 )], G14 => [qw( Galpha-14 )], G15 => [qw( Galpha-15 )], G16 => [qw( Galpha-16 )], Gs => [qw( Galpha-s )], Gz => [qw( Galpha-z )], Golf => [qw( Galpha-olf )], Go => [qw( Galpha-o )], Gq => [qw( Galpha-q )], Gi => [qw( Galpha-i1 Galpha-i2 Galpha-i3 )], Gt => [qw( Galpha-t1 Galpha-t2 Galpha-t3 )], ); my $re = qr{ \A ( [^\t]* \t [^\t]* ) \t ( [^\t]* ) \t ( [^\t]* \t .* ) \z }msx; LINE: while ( my $line = <DATA> ) { chomp $line; my ( $col_1_2, $col_3, $col_rest ) = ( $line =~ /$re/ ) or do { warn "Line $. did not match: '$line'"; next LINE; }; $col_3 =~ tr{ }{}d; my $aref = $gp{$col_3} or do { # No replacement needed? Is line still wanted? warn "Did not find '$col_3' in %gp"; print $line, "\n"; next LINE; }; for my $val ( @{ $aref } ) { print join( "\t", $col_1_2, $val, $col_rest ), "\n"; } } __DATA__ biologist xargon Gi question col5 perl monks G11 answer? col5


Comment on Re: How to improve my code? main concern:array as hash element
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2014-11-24 14:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (141 votes), past polls