Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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

by Ganesh Bharadwaj1 (Sexton)
on Dec 29, 2015 at 02:19 UTC ( [id://1151303]=perlquestion: print w/replies, xml ) Need Help??

Ganesh Bharadwaj1 has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

Let us call the input file shouldIgo.txt. My input file looks like this.

 Jakarta paris delhi singapore

 hot cold wet

 yes no cannotcomment

I want to generate multiple output files which have the information which says

output file 1 is

city: Jakarta

weather: hot

recommended? : yes

output file 2 will be

city: Jakarta

weather: hot

recommended? : no

and so on. City, weather and recommended are hard coded. totally there will be 4*3*3 files. containing all possible combinations. Each of these information needs to be stored in separate files. I think I need to use file handles and manipulate the array. I am not able to achieve the result. Any help on this direction will be helpful.

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

Replies are listed 'Best First'.
Re: Get an input file and print multiple output files after performing operations on array.
by BrowserUk (Patriarch) on Dec 29, 2015 at 03:08 UTC
    #! perl -slw use strict; my @city = split ' ', <DATA>; my @temp = split ' ', <DATA>; my @note = split ' ', <DATA>; my $i = '01'; for my $city ( @city ) { for my $temp ( @temp ) { for my $note ( @note ) { open O, '>', 'out' . $i++ or die $!; print O 'city: ', $city; print O 'weather: ', $temp; print O 'recommended: ', $note; close O; } } } __DATA__ Jakarta paris delhi singapore hot cold wet yes no cannotcomment

    Output:


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.
      thank you soo much. That works like a charm :)
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

    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:

    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,

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (7)
As of 2024-04-23 20:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found