Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

++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


In reply to Re: How to improve my code? main concern:array as hash element by Util
in thread How to improve my code? main concern:array as hash element by xargon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-25 23:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found