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

Howdy, I have a file with 2968 commands (on different lines and all ending with \n) in a file named "subfile". I would like to put each of the commands in their own file (i.e. subfile0, subfile1, ... , up to subfile2967) Heres how far ive got.....
#!/usr/bin/perl -W $NumOfSim = shift(@ARGV); chomp($me = `id -un`); $PATH = "/home/$me/src/Tc/Scheme4/Simulation$NumOfSim"; open(FILEIN,"<$PATH/subfile") || die "Cannot open subfile\n"; @row_data = <FILEIN>; close FILEIN; for($j=0;$j<2968;$j++) # jmax = number of job subs { open(FILEOUT,">$PATH/subfile$j") || die "Cannot open subfile$j \n"; foreach $line(@row_data) { chomp($line); $cmd = "$line "; print FILEOUT "$cmd \n"; }#$line }
...when i try to print each command to its own file, ALL the commands go to ALL the files ?? I know ive messed up on the loops but confused where it went wrong and confused how to fix it. im a perl virgin, pls be kind.

Replies are listed 'Best First'.
Re: loop da loop chomp chomp loop chomp blurp oops
by Erez (Priest) on Feb 02, 2008 at 16:25 UTC

    Your for loop opens a file then the foreach loop writes all the $lines in @row_data.
    Move the open statement inside the foreach loop, and discard the for loop. That should get you going.

    Software speaks in tongues of man; I debug, therefore I code.
    Stop saying 'script'. Stop saying 'line-noise'.

Re: loop da loop chomp chomp loop chomp blurp oops
by lidden (Curate) on Feb 02, 2008 at 19:06 UTC
    You want something like this (untested):
    use strict; use warnings; my $num_of_sim = shift; my $me; chomp($me = `id -un`); my $PATH = "/home/$me/src/Tc/Scheme4/Simulation$num_of_sim"; open my $fh ,'<', "$PATH/subfile" or die "Cannot open subfile: $!"; my @row_data = <$fh>; close $fh or die $!; my $i = 0; for my $line (@row_data) { open my $file_out, '>', "$PATH/subfile$i" or die "Cannot open subf +ile$i: $!"; print $file_out $line; $i++; # last if $i > 2967 # Needed if the file has more then 2968 lines }
Re: loop da loop chomp chomp loop chomp blurp oops
by jwkrahn (Abbot) on Feb 02, 2008 at 19:35 UTC

    You probably want something like this:

    #!/usr/bin/perl use warnings; use strict; my $NumOfSim = shift or die "usage: $0 NumOfSim\n"; my $me = getpwuid $<; my $PATH = "/home/$me/src/Tc/Scheme4/Simulation$NumOfSim"; open my $FILEIN, '<', "$PATH/subfile" or die "Cannot open subfile: $!" +; my $j = 0; while ( my $line = <$FILEIN> ) { open my $FILEOUT, '>', "$PATH/subfile$j" or die "Cannot open subfi +le$j: $!"; $line =~ s/$/ /; print $FILEOUT $line; last if ++$j >= 2968; }
Re: loop da loop chomp chomp loop chomp blurp oops
by toolic (Bishop) on Feb 02, 2008 at 19:21 UTC
    I believe this can also be done on *nix with the split command.
    split -1 /path/to/input_file.txt subfile
      A small change gives the requested numerical file names:
          split -d1a 4 /path/to/input_file.txt subfile
      Still not exactly as specified since the names will be zero-padded but it keeps them sorting alphanumerically ... and that's probably better anyway.

      OK, this ain't Perl but a one-liner is a one-liner.

Re: loop da loop chomp chomp loop chomp blurp oops
by apl (Monsignor) on Feb 03, 2008 at 19:03 UTC
    You might also want to do a close( FILEOUT ) or die("Couldn't close file $i: $!"); after printing the single line, before opening the next output file.
Re: loop da loop chomp chomp loop chomp blurp oops
by pc88mxer (Vicar) on Feb 03, 2008 at 19:37 UTC
    May I ask why you want to create ~3K files? May I ask the proverbial WAYRTTD (what are you really trying to do)?