From: activeperl-boun...@listserv.activestate.com 
[mailto:activeperl-boun...@listserv.activestate.com] On Behalf Of zilore mumba
Sent: 10 February 2012 17:33
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
>
>
> #!/usr/bin/perl -w
> use strict;
> use warnings;
>
>  open (IN, "< text1") or die "open 'text1' failed: $!\n";
>  open (OUT, ">$text2") or die "open 'text1' failed: $!\n";
>
>  # Copy first 3 lines unchanged.
>  print OUT scalar(<IN>) for 1..3;
>
>  # Reformat the numbers on each remaining line.
>
>  while (<IN>)
>  {
>   my ($month,$day,$year,$hour,$min,@rain) = split;
>
> # printf ("%04d%02d%02d%02d%02d\n",$year,$month,$day,$hour,$min);
>   $_ = sprintf "%5.1f", $_ foreach @rain;
>
>   print OUT ($year,$month,$day,$hour,$min, join(" ", @rain), "\n");

Did you mean like ...

print OUT join(" ", $year,$month,$day,$hour,$min, @rain), "\n";


>  }
>
>  # Close files
>  close IN or die "close IN: $!\n";
>  close OUT or die "close OUT 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
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to