How can I write a Perl script that reads from another file?

后端 未结 3 941
一生所求
一生所求 2021-01-27 22:58

I want to write a Perl Script that reads a file.txt with columns of numbers,

20  30  12
31  20  54
63  30  21
11  12  10

and do some calculatio

3条回答
  •  太阳男子
    2021-01-27 23:35

    Here are suggested functions to use (see the function reference):

    1. Open your file for reading: use the open function
    2. Loop through each line: while (my $line = <$filehandle>)
    3. Remove the trailing newline: use chomp
    4. Extract the values from each line: use the split function
    5. Store the values in an array: use push

    To verify that your array has what you want at the end:

    use Data::Dumper;
    print Dumper \@vals;
    

    UPDATE

    Without giving you the entire answer (since this is homework) have a look at the sample code in each entry of the function reference.

    Here's something to get you started:

    open my $filehandle, '<', $filename
        or die "Couldn't open $filename";
    while (my $line = <$filehandle>) {
        # do stuff with $line
    }
    close $filehandle;
    

提交回复
热议问题