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

dear monks. I have a file in which I am getting the input from. I wish to generate output files and store these output files in different folders. as in out1.txt should be stored in directory1 and out2.txt needs to be stored in directory2 and so on. Using the current code I am able to create all possible combinations of the values present in my input code and put them in different files. My input file looks like

Delhi Beijing Canberra NewYork hot cold rainy go dontgo cannotcomment
One sample output file will look like
City : Delhi Weather : hot recommended : go
another will look like
City : Delhi Weather : hot recommended : dontgo
I want to print all possible combinations of the of city, weather and recommended , and the code below does that. But I also want to take each output file and put it in a different folder. Which is where I am having the issue. I think I should use the makepath keyword and use it inside the loop. But once I use the use File::Path, it gives the error :
syntax error at perl_script.pl line 8, near ") or"
Line 8 just opens the file for input. This is why I am confused. Could you please help let me know how I can modify the code to put the files in different folders?
#! perl -slw use strict; use Data::Dumper; #use File::Path open(my $in, '<', 'ocean_dummy') or die "Cannot open input.txt: $!"; my @city = split ' ', <$in>; my @temp = split ' ', <$in>; my @note = split ' ', <$in>; #perl -MFile::Path -e 'mkpath([map { "dir$_" } (1 .. 20)])' #-e 'mkpath([map { "dir$_" } (1 .. 20)])' #mkpath([map { "dir$_" } (1 .. 20)]); my $i = '01'; for my $city ( @city ) { for my $temp ( @temp ) { for my $note ( @note ) { #mkdir directory_$city$temp$note open O, '>', 'out' . $i++ or die $!; print O 'city: ', $city; print O 'weather: ', $temp; print O 'recommended: ', $note; close O; } } }

Replies are listed 'Best First'.
Re: perform array manipulation and save files in different directories.
by 1nickt (Canon) on Dec 30, 2015 at 03:33 UTC

    As pointed out above, the error is caused by the missing semi-colon on your now-commented use statement.

    Other suggestions:

    • If you are going to use directories to separate the files, they don't need to have the same numbers in the file names. I would prefer:
      ->dir1->file.txt ->dir2->file.txt ->dir3->file.txt
      or:
      ->dir->file1.txt ->file2.txt ->file3.txt
      But it is redundant to have:
      ->dir1->file1.txt ->dir2->file2.txt ->dir3->file3.txt
    • You can't use perl -e inside a script: that's for invoking perl on the command line.
    • map() is just a fancy for loop that you don't always need:
      use File::Path qw/ make_path /; make_path( $_ ) for 1 .. 20;

    Hope this helps!


    The way forward always starts with a minimal test.
      thank you for steering me in the right direction. I could make it work :)
Re: perform array manipulation and save files in different directories. syntax error at perl_script.pl line 8, near ")
by Anonymous Monk on Dec 30, 2015 at 02:53 UTC
    you forgot semicolon, syntas is use modulename;## semicolon