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:

    C:\test>type out* out01 city: Jakarta weather: hot recommended: yes out02 city: Jakarta weather: hot recommended: no out03 city: Jakarta weather: hot recommended: cannotcomment out04 city: Jakarta weather: cold recommended: yes out05 city: Jakarta weather: cold recommended: no out06 city: Jakarta weather: cold recommended: cannotcomment out07 city: Jakarta weather: wet recommended: yes out08 city: Jakarta weather: wet recommended: no out09 city: Jakarta weather: wet recommended: cannotcomment out10 city: paris weather: hot recommended: yes out11 city: paris weather: hot recommended: no out12 city: paris weather: hot recommended: cannotcomment out13 city: paris weather: cold recommended: yes out14 city: paris weather: cold recommended: no out15 city: paris weather: cold recommended: cannotcomment out16 city: paris weather: wet recommended: yes out17 city: paris weather: wet recommended: no out18 city: paris weather: wet recommended: cannotcomment out19 city: delhi weather: hot recommended: yes out20 city: delhi weather: hot recommended: no out21 city: delhi weather: hot recommended: cannotcomment out22 city: delhi weather: cold recommended: yes out23 city: delhi weather: cold recommended: no out24 city: delhi weather: cold recommended: cannotcomment out25 city: delhi weather: wet recommended: yes out26 city: delhi weather: wet recommended: no out27 city: delhi weather: wet recommended: cannotcomment out28 city: singapore weather: hot recommended: yes out29 city: singapore weather: hot recommended: no out30 city: singapore weather: hot recommended: cannotcomment out31 city: singapore weather: cold recommended: yes out32 city: singapore weather: cold recommended: no out33 city: singapore weather: cold recommended: cannotcomment out34 city: singapore weather: wet recommended: yes out35 city: singapore weather: wet recommended: no out36 city: singapore weather: wet recommended: cannotcomment

    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,