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:
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?
In reply to Re: How to improve my code? main concern:array as hash element
by hbm
in thread How to improve my code? main concern:array as hash element
by xargon
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |