- #1
arcTomato
- 105
- 27
- TL;DR Summary
- How to open multiple files and print different content to each files.
Hello PF!
I would like to know how to open multiple files and print different content to each files.
The problem is $line_no1,2.
means, I tried to write $line_no1,2 as a different value for each of files(tmp1~4.pco) using a while and for statement.
I tries like this but dosen't work...
The files(tmp1~4.pco) can be created, but not contents...
Thank you
Arctomato
I would like to know how to open multiple files and print different content to each files.
The problem is $line_no1,2.
means, I tried to write $line_no1,2 as a different value for each of files(tmp1~4.pco) using a while and for statement.
I tries like this but dosen't work...
The files(tmp1~4.pco) can be created, but not contents...
Perl:
my $infile=$ARGV[0];
my $start_no=$ARGV[1];
open (my $infh,'<',$infile);
my $i=0;
my $j;
for ($j=1;$j<5;$j++){
my $outfile1="tmp1.pco";
my $outfile2="tmp2.pco";
my $outfile3="tmp3.pco";
my $outfile4="tmp4.pco";
open (my $outfl1,'>',$outfile1);
open (my $outfl2,'>',$outfile2);
open (my $outfl3,'>',$outfile3);
open (my $outfl4,'>',$outfile4);
while ( my $line= <$infh>) {
$i++;
if ($i == 1){
next;#skip label
}
my $line_no1=2*($start_no-2)+2;
my $line_no2=$line_no1+1;
if($j==1){
print $outfl1;
print $outfl1 "$line_no1 \n";
print $outfl1 "$line_no2 \n";
}
elsif($j==2){
print $outfl2 "$line_no1 \n";
print $outfl2 " $line_no2 \n";
}
elsif($j==3){
print $outfl3 "$line_no1 \n";
print $outfl3 "$line_no2 \n";
}
else{
print $outfl4 "$line_no1 \n";
print $outfl4 " $line_no2 \n";
}
$start_no++;
}
close ($outfl1);
close ($outfl2);
close ($outfl3);
close ($outfl4);
}
close ($infh);
Thank you
Arctomato