Beefy Boxes and Bandwidth Generously Provided by pair Networks httptech
more useful options
 
PerlMonks  

Re: Nested Loop Problems

by johngg (Abbot)
on May 02, 2012 at 21:33 UTC ( #968552=note: print w/ replies, xml ) Need Help??


in reply to Nested Loop Problems

It is possible to populate the four arrays in one go by pushing onto the correct array as decided by nested ternaries. However, it doesn't look particularly clean and simple and you are probably better off to start with following ww's suggestion to break the problem into two stages. Here's one way to do it in one fell swoop.

knoppix@Microknoppix:~$ perl -Mstrict -wE ' > open my $fh, q{<}, \ <<EOD or die $!; > name1,a1,b1,c1 > name2,a2,b2,c2 > name3,a3,b3,c3 > EOD > > my( @c1, @c2, @c3, @c4 ); > while ( <$fh> ) > { > chomp; > my $idx = 0; > push @{ > $_->[ 0 ] == 1 ? \ @c1 > : $_->[ 0 ] == 2 ? \ @c2 > : $_->[ 0 ] == 3 ? \ @c3 > : \ @c4 }, $_->[ 1 ] for > map { [ ++ $idx, $_ ] } > split m{,}; > } > > say q{}; > say qq{@c1}; > say qq{@c2}; > say qq{@c3}; > say qq{@c4};' name1 name2 name3 a1 a2 a3 b1 b2 b3 c1 c2 c3 knoppix@Microknoppix:~$

I hope this is of interest.

Update: corrected broken link.

Cheers,

JohnGG


Comment on Re: Nested Loop Problems
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://968552]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (13)
As of 2014-04-17 14:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (450 votes), past polls