in reply to read text file and generate an output file with information present in text file.

You are not that far off

#!perl use strict; # <--- add open(my $fh, '<', 'Citytempfile.txt') or die "Cannot open Citytempfile.txt: $!"; # ^^^^ corrected my $i = 0; # ^ missing ; while (<$fh>) { #my @array = split ' ', <$fh> my @array = split ' ', $_; # change print $i+1, '. Temperature in ',$array[0], ' is ', $array[1], ' but average temp in ', $array[2], ' is ', $array[3]," degrees.\n" ; $i++; }

Take a look at using printf

poj
  • Comment on Re: read text file and generate an output file with information present in text file.
  • Download Code

Replies are listed 'Best First'.
Re^2: read text file and generate an output file with information present in text file.
by 2teez (Vicar) on Jan 28, 2016 at 09:34 UTC

    Or Instead of using my $i = 0;
    Could use $INPUT_LINE_NUMBER $.
    like so:

    while (...) { print $., .... }
    And perl take care of the rest for you!

    Update:

    Need I mention, that to use the full name as $INPUT_LINE_NUMBER you have to use English; module.
    like so:

    use warnings; use strict; use autodie qw/open close/; use English; open my $fh, '<', $ARGV[0]; while ( my $line = <$fh> ) { print $INPUT_LINE_NUMBER, " ", $line; } close $fh;

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me