Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

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

by hbm (Hermit)
on Nov 23, 2011 at 16:30 UTC ( #939694=note: print w/replies, xml ) Need Help??

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

If your code is very representative, it seems you can do away with your hash and arrays, and do something like this where you first assign $j:

($j=$i) =~ s/G(.+)/Galpha-$1/; my @values = ($j =~ /(Galpha-[it])/ ? map{$1.$_}1..3 : ($j) );

Also, read up on modifiers in perlre. You have this:

if ($_=~/^.*\t.*\t(.*)\t.*\t.*/mgi)

All the modifiers are unnecessary:

  • m, because you are matching against a single line;
  • g, because you match the (one) beginning of the line;
  • i, because you are matching .*, i.e., in any case


Update: Clearly, I don't have a clue what your data looks like, but I'm thinking something like this:

use strict; use warnings; while (<DATA>){ chomp; next unless my $i = (split/\t/,$_)[2]; my $f = $_; $i =~ s/ //g; (my $j = $i) =~ s/G(.+)/Galpha-$1/; my @values = ($j =~ /(Galpha-[it])/ ? map{$1.$_}1..3 : ($j) ); for my $val (@values) { my $k = $f; $k =~ s/$i/$val/g; print "$k\n"; } } __DATA__ biologist xargon Gi question perl monks G11 answer? # prints biologist xargon Galpha-i1 question biologist xargon Galpha-i2 question biologist xargon Galpha-i3 question perl monks Galpha-11 answer?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://939694]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (1)
As of 2018-07-22 09:24 GMT
Find Nodes?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

    Results (453 votes). Check out past polls.