Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

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]
Eily notes also
[hippo]: So if you don't drink as much milk and not as often you are said to drink less milk on fewer days. Confusing, eh?
[Eily]: same as many/much, few/little
[Discipulus]: so my 'less' usage sounded not so correct? 'fewer' was more appropriate? i take a note too
Discipulus $ENV{PAGER}='fewer '
[Corion]: Discipulus: Hahahaa! ;)

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2018-01-23 09:12 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (242 votes). Check out past polls.