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

In summary: OUT;In summary, the problem is that you are not printing the input line number with the label. You are also not closing the output files.
  • #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...
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
 
Technology news on Phys.org
  • #2
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
Svein said:
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
  • #4
Thank you @Mark44, @Svein!
Ok, I should check the command line part.
 
  • #5
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
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:

1. How do I open multiple files in Perl?

To open multiple files in Perl, you can use the open function. This function takes two arguments: the file handle and the file name. You can use a loop to open multiple files and assign a different file handle to each one. For example:

foreach $file ("file1.txt", "file2.txt", "file3.txt") {
open(my $fh, '<', $file) or die "Could not open file $file: $!";
# Do something with $fh
close($fh);
}

2. How can I print different content from each file in Perl?

To print different content from each file in Perl, you can use the print function. This function takes the file handle as its argument, so you can use a loop to print the content of each file using its corresponding file handle. For example:

foreach $file ("file1.txt", "file2.txt", "file3.txt") {
open(my $fh, '<', $file) or die "Could not open file $file: $!";
while (my $line = <$fh>) {
print $line;
}
close($fh);
}

3. How do I check if a file exists before opening it in Perl?

To check if a file exists before opening it in Perl, you can use the -e file test operator. This operator returns a true value if the file exists and a false value if it does not. For example:

if (-e "file.txt") {
# File exists, open it
} else {
# File does not exist, handle the error
}

4. Can I open files in Perl for both reading and writing?

Yes, you can open files for both reading and writing in Perl using the + mode. This mode allows you to read from and write to the same file handle. For example:

open(my $fh, '+<', "file.txt") or die "Could not open file: $!";
# Read from file
while (my $line = <$fh>) {
print $line;
}
# Write to file
print $fh "New content";
close($fh);

5. How can I handle errors when opening multiple files in Perl?

To handle errors when opening multiple files in Perl, you can use the die function. This function prints an error message and terminates the program. You can use it in conjunction with the open function to handle errors when opening files. For example:

foreach $file ("file1.txt", "file2.txt", "file3.txt") {
open(my $fh, '<', $file) or die "Could not open file $file: $!";
# Do something with $fh
close($fh);
}

Similar threads

  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
18
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
944
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
19
Views
5K
Back
Top