Thanks to all those who looked at my code and particularly to Francisco, Ken 
and Brian. I have tried your suggestions and they do exactly what I want to do. 
My appreciation for your time.
Zilore



________________________________
 From: Francisco Zarabozo <fzarab...@hotmail.com>
To: zilore mumba <zmu...@yahoo.com>; 'Active State Perl Mailing List' 
<activeperl@listserv.ActiveState.com> 
Sent: Friday, February 10, 2012 7:53 PM
Subject: Re: help formatting string
 

Hi Zilore,

> From: zilore mumba
> Sent: Friday, February 10, 2012 11:32 AM
> To: 'Active State Perl Mailing List'
> Subject: help formatting string
>
> Please help with some very simple code below which somehow I a, not 
> getting right.
> I have strings consisting of $day, $month, $year, $hour, $min, followed by 
> some float values which I call @rain, as in the attached file 'text1.txt'.
> I want rewrite this data as $year, $month, $day, $hour, $min,  @rainin as 
> in  'text2.txt', but with the date also formatted to have one space 
> between each field
> Help will be appreciated.
> Zilore


I think the following code does exactly what you wanted it to do:

use strict;
use warnings;

open my $IN, '<', "text1.txt" or die "open 'text1.txt' failed: $!\n";
open my $OUT, '>', "text2.txt" or die "open 'text2.txt' failed: $!\n";

# Copy first 3 lines unchanged.
print $OUT scalar(<$IN>) for 1..3;

# Reformat the numbers on each remaining line.
while (my $line = <$IN>) {
    $line =~ s/^\s+|\s+$//g; # trim it before split since rows start with 
spaces
    my ($month, $day, $year, $hour, $min, @rain) = split /\s+/, $line;
    $_ = sprintf("%05.1f", $_) for @rain;
    print $OUT sprintf("%04d %02d %02d %02d %02d ", $year, $month, $day, 
$hour, $min).join(' ', @rain)."\n";
}

# Close files
close $IN or die "close IN failed: $!\n";
close $OUT or die "close OUT failed: $!\n";

-----------------------------------------------------
The output is all even to the view. These are the first 2 rows:

2012 01 01 12 00 096.5 098.2 099.8 001.4 013.3 013.5 013.6 000.1 849.2 849.2 
849.3 000.0 000.0 000.0 000.0 000.0 000.0 012.6 014.1 000.9 002.5 004.8 
000.7 050.0 051.0 002.0 014.0 000.0
2012 01 01 12 10 099.0 099.2 099.5 000.2 013.3 013.4 013.4 000.0 849.2 849.2 
849.3 000.0 000.0 000.0 000.0 000.0 000.0 012.7 014.1 000.7 002.0 003.4 
000.5 050.0 050.0 002.0 012.0 000.0



HTH

Francisco Zarabozo

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to