Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Get an input file and print multiple output files after performing operations on array.

by Athanasius (Archbishop)
on Dec 29, 2015 at 13:07 UTC ( [id://1151350]=note: print w/replies, xml ) Need Help??


in reply to Get an input file and print multiple output files after performing operations on array.

Hello Ganesh Bharadwaj1, and welcome to the Monastery!

BrowserUk has answered your question; but, in case you ever need a more general solution, tye’s module Algorithm::Loops provides the NestedLoops function for this kind of task:

#! perl use strict; use warnings; use Algorithm::Loops qw( NestedLoops ); use Data::Dump; my @AoA; while (<DATA>) { chomp; my @fields = split; push @AoA, \@fields; } dd \@AoA; NestedLoops ( [ @AoA[0 .. $#AoA] ], sub { print join(', ', @_), "\n"; }, ); __DATA__ Jakarta paris delhi singapore hot cold wet yes no cannotcomment black white

Output:

22:59 >perl 1500_SoPW.pl [ ["Jakarta", "paris", "delhi", "singapore"], ["hot", "cold", "wet"], ["yes", "no", "cannotcomment"], ["black", "white"], ] Jakarta, hot, yes, black Jakarta, hot, yes, white Jakarta, hot, no, black Jakarta, hot, no, white Jakarta, hot, cannotcomment, black Jakarta, hot, cannotcomment, white Jakarta, cold, yes, black Jakarta, cold, yes, white Jakarta, cold, no, black Jakarta, cold, no, white Jakarta, cold, cannotcomment, black Jakarta, cold, cannotcomment, white Jakarta, wet, yes, black Jakarta, wet, yes, white Jakarta, wet, no, black Jakarta, wet, no, white Jakarta, wet, cannotcomment, black Jakarta, wet, cannotcomment, white paris, hot, yes, black paris, hot, yes, white paris, hot, no, black paris, hot, no, white paris, hot, cannotcomment, black paris, hot, cannotcomment, white paris, cold, yes, black paris, cold, yes, white paris, cold, no, black paris, cold, no, white paris, cold, cannotcomment, black paris, cold, cannotcomment, white paris, wet, yes, black paris, wet, yes, white paris, wet, no, black paris, wet, no, white paris, wet, cannotcomment, black paris, wet, cannotcomment, white delhi, hot, yes, black delhi, hot, yes, white delhi, hot, no, black delhi, hot, no, white delhi, hot, cannotcomment, black delhi, hot, cannotcomment, white delhi, cold, yes, black delhi, cold, yes, white delhi, cold, no, black delhi, cold, no, white delhi, cold, cannotcomment, black delhi, cold, cannotcomment, white delhi, wet, yes, black delhi, wet, yes, white delhi, wet, no, black delhi, wet, no, white delhi, wet, cannotcomment, black delhi, wet, cannotcomment, white singapore, hot, yes, black singapore, hot, yes, white singapore, hot, no, black singapore, hot, no, white singapore, hot, cannotcomment, black singapore, hot, cannotcomment, white singapore, cold, yes, black singapore, cold, yes, white singapore, cold, no, black singapore, cold, no, white singapore, cold, cannotcomment, black singapore, cold, cannotcomment, white singapore, wet, yes, black singapore, wet, yes, white singapore, wet, no, black singapore, wet, no, white singapore, wet, cannotcomment, black singapore, wet, cannotcomment, white 23:00 >

As you can see, this approach allows you to add another line to your input file without making any changes to your code!

Hope that’s of interest,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

  • Comment on Re: Get an input file and print multiple output files after performing operations on array.
  • Select or Download Code

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-26 02:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found