Re: Reading a line as an array

zilore mumba <[email protected]>
Newsgroups gmane.comp.lang.perl.active-perl
Message-ID <[email protected]>
Brian,
Thanks, the code you sent does exactly what I want.
Thanks for your time and thanks to all those who looked at my code.
Zilore



----- Original Message ----
From: Brian Raven <[email protected]>
To: [email protected]
Sent: Wed, September 22, 2010 10:57:40 AM
Subject: RE: Reading a line as an array

zilore mumba <> wrote:
> Excuse me for the missing attachments. As I composed the mail in
> gmail which is not recognised on the mailing list. The attachments
> are now included.  

The main problem with your code, in addition to Jusin's point about file
handle naming, is with the line:

    my @rain = <OUT>; 

which reads the rest of the file into the array @rain, which is clearly
not what you intend.

The following seems to do what you are asking about.

-------------------------------------------------
use strict;
use warnings;

# You might want to initialise the file names from the command line.
my $in_fn = "sample.txt";
my $out_fn = "sample_tr.txt";

# The preferred open is the 3 arg method with a localised file handle.
open my $infd, "<", $in_fn or die "open $in_fn failed: $!\n";
open my $outfd, ">", $out_fn or die "open $out_fn failed: $!\n";

# Copy first 7 lines unchanged.
print $outfd scalar(<$infd>) for 1..7;

# Reformat the numbers on each remaining line.
while (<$infd>) {
    my ($year, $month, @rain) = split;
    $_ = sprintf "%.02f", $_ foreach @rain;
    print $outfd "\t$year $month\t", join("\t", @rain), "\n";
}

# Close files. Don't forget that closing an output file can fail.
close $infd;
close $outfd or die "close $out_fn failed: $!\n";
-------------------------------------------------------

HTH

-- 
Brian Raven 

Please consider the environment before printing this e-mail.

This e-mail may contain confidential and/or privileged information. If you are 
not the intended recipient or have received this e-mail in error, please advise 
the sender immediately by reply e-mail and delete this message and any 
attachments without retaining a copy.

Any unauthorised copying, disclosure or distribution of the material in this 
e-mail is strictly forbidden.
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs



      
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.