Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

comment on

( #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

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (4)
    As of 2020-07-14 09:49 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found