Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by Ganesh Bharadwaj1 (Sexton)
on Jan 28, 2016 at 08:56 UTC ( [id://1153829]=perlquestion: print w/replies, xml ) Need Help??

Ganesh Bharadwaj1 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks, I have an input file which has 4 entries per line. But I am not sure how many lines will be there in the input file.
Jakarta 30 indonesia 40 Beijing 50 China 20
I want to print out something like
1. Temperature in Jakarta is 30, but average temp in indonesia is 40 d +egrees. 2. Temperature in Beijing is 50, but average temp in China is 20 degre +es.
I am not sure how to save the 4 words and split them using delimiter so that I can actually use them somewhere. I think I am way off in the coding part, since I am new to perl. This is what I wrote, but this doesnt work.
open(my $fh, '<', 'Citytempfile.txt') or die "Cannot open cellnames.txt: $!"; my $i = 0 while (<$fh>) { my @array = split ' ', <$fh> print $fileHandle_drc $i+1, 'Temperature in ',$array[0], ' is ', $arr +ay[1], 'but average temp in ', $array[2], 'is', $array[3] ; $i++; }

Replies are listed 'Best First'.
Re: read text file and generate an output file with information present in text file.
by poj (Abbot) on Jan 28, 2016 at 09:25 UTC

    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

      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
Re: read text file and generate an output file with information present in text file.
by thomas895 (Deacon) on Jan 28, 2016 at 09:23 UTC

    You say that "it doesn't work" - what is it doing that you don't like?

    Some things:

    1. Where is $fileHandle_drc coming from?
    2. Did you try useing strict and warnings?

    -Thomas
    "Excuse me for butting in, but I'm interrupt-driven..."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1153829]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-25 09:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found