I think this is what you want. @resultarray in this code will contain a list of numbers.
If you give your program the name of the file that will be used as input from the command line, remove it from the ARGV array and open it as a file descriptor:
my $filename = $ARGV[0]; open(my $filehandle, '<', $filename) or die "Could not open $filename\n";
Once you have a file descriptor, you can iterate over each line of the file using a while loop. chomp this new line character from the end of each line. Use split to separate each line based on spaces. This returns an array ( @linearray in my code) containing a list of numbers inside this line. I then push my array of strings at the end of my @resultarray .
my @resultarray; while(my $line = <$filehandle>){ chomp $line; my @linearray = split(" ", $line); push(@resultarray, @linearray); }
And don't forget to add
use warnings; use strict;
at the top of your perl file to help you if you run into problems with your code.
Just explain how you can work with the various inputs to your program. Next command:
perlfile.pl < inputfile.txt
Take the contents of the inputfile.txt file and pass it to the STDIN file descriptor. Then you can use this file descriptor to access the contents of inputfile.txt
while(my $line = <STDIN>){
But you can also give your program several readable file names by placing them after the run command:
perlfile.pl inputfile1.txt inputfile2.txt
These file names will be read as strings and placed in the @ARGV array so that the array looks like this:
@ARGV: [0] => "inputfile1.txt" [1] => "inputfile2.txt"
Since these are just file names, you need to open the file in perl before accessing the contents of the file. So for inputfile1.txt:
my $filename1 = shift(@ARGV); open(my $filehandle1, '<', $filename1) or die "Can't open $filename1"; while(my $line = <$filehandle1>){
Notice how I used shift this time to get the next element in the @ARGV array. See perldoc for details . As well as additional information about open .