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

  • Thread starter Thread starter arcTomato
  • Start date Start date
  • Tags Tags
    files Multiple
Click For Summary

Discussion Overview

The discussion revolves around a Perl programming issue related to opening multiple files and printing different content to each file. Participants explore the code provided by the original poster, identify potential problems, and suggest modifications to achieve the desired functionality.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • The original poster, Arctomato, describes an issue with their Perl script where files are created but not populated with content as intended.
  • Some participants note that in C, the behavior of command line arguments differs from Perl, suggesting that $ARGV[0] and $ARGV[1] may not function as expected.
  • One participant suggests moving the opening of file handles outside the loop to avoid repeatedly opening them, which could be causing issues.
  • Another participant questions whether the original poster intends to print the line number or the actual input line, indicating a lack of clarity in the original code.
  • There is a suggestion to include comments in the code to clarify its purpose, which may help in troubleshooting and understanding the logic.

Areas of Agreement / Disagreement

Participants express varying levels of familiarity with Perl, leading to multiple interpretations of the code and its intended functionality. There is no consensus on the best approach to resolve the original poster's issue, as different suggestions are offered without agreement on a single solution.

Contextual Notes

Some participants highlight the importance of understanding how command line arguments are handled in Perl compared to C, which may affect the behavior of the script. Additionally, the original code's logic and intended output remain somewhat ambiguous, leading to different interpretations and suggestions.

arcTomato
Messages
104
Reaction score
27
TL;DR
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 doesn'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
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   Reactions: arcTomato
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   Reactions: arcTomato
Thank you @Mark44, @Svein!
Ok, I should check the command line part.
 
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.
 
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.
[CODE lang="perl" title="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
}[/CODE]
 
Last edited:

Similar threads

  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 16 ·
Replies
16
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
1
Views
2K
  • · Replies 19 ·
Replies
19
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K