[Perl] How to open multiple files and print different content

  • Thread starter arcTomato
  • Start date
  • #1
82
25

Summary:

How to open multiple files and print different content to each files.

Main Question or Discussion Point

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.....


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
 

Answers and Replies

  • #2
Svein
Science Advisor
Insights Author
2,079
668
I do not know Perl, but in C "argv[0]" returns the (fully qualified) name of the program itself. The first parameter is returned in "argv[1]".
 
  • Like
Likes arcTomato
  • #3
33,631
5,285
I do not know Perl, but in C "argv[0]" returns the (fully qualified) name of the program itself. The first parameter is returned in "argv[1]".
I don't know Perl, either, but based on three different web tutorials, argv[0] contains the first command line parameter. If the program is started with no command line parameters, the argv array will be empty. IOW, the behavior is different from C and C++.
 
  • Like
Likes arcTomato
  • #5
140
92
I think part of the problem is that you are opening the file handles every time you step through the
"for ($j=1;$j<5;$j++){" loop.

Move start of that loop down to just above the start of the "while" loop, below where the file handles are opened, and try again.

FWIW in Perl the special variable "$0" contains the filename of the currently running script.
 
  • #6
FactChecker
Science Advisor
Gold Member
5,578
2,056
I hope that you don't mind if I start you off with a code that I hope starts you on what you want to do. I think that any new Perl programmer benefits from that. I don't see anywhere that you were reading from the input file, so it is not really clear what you wanted to print in each file. For your print statements, do you want to print the line number, $line_no1, or the actual input line, $inlines[$line_no1]? Also, one beginner mistake is to not have any comments on what the lines in your code are trying to do. That is especially bad when you want help, but it is always better to put comments in your code.
Starter Perl code:
my $infile=$ARGV[0];
my $original_start_no=$ARGV[1];

# read lines of input file into an array
@inlines = split(/\n/, `type $infile`);
$label_line = shift @inlines; #get label

# first line of data file is now at Perl array index 0
$line_no= $original_start_no-2;
foreach $j (1..4){ # loop to create 4 output files
    my $outfile="tmp$j.pco";  # make output file name
    open(OUT, ">$outfile") or die "Can't open $outfile to write\n"; # open output file
    print OUT "$label_line\n";  # put labels on first line of each file
    print OUT "$inlines[$line_no]\n"; # print input line line_no.
    $line_no++; # move line number to next line
    print OUT "$inlines[$line_no]\n"; # print the next line
    $line_no++; # move line number so next file will begin there
    close OUT;  # close this output file
}
 
Last edited:

Related Threads on [Perl] How to open multiple files and print different content

  • Last Post
Replies
4
Views
3K
Replies
1
Views
748
  • Last Post
Replies
1
Views
2K
Replies
1
Views
3K
  • Last Post
Replies
12
Views
905
Replies
4
Views
3K
Replies
2
Views
2K
Top