On Tuesday 20 November 2007 14:00, Gerald Wheeler wrote:
> Running Perl 5.6.1 on Solaris 9 SPARC
> No (access to) modules available other than what comes with the basic
> perl installation.
>
> I have a file: ifiln.csv as such:
> Julian Day of the year, value
> 1,4144.34
> 2,4144.38
> 3,4144.38

[ SNIP ]

So far, so good.


> 366,4147.56 (Leap Year)

Is the text ' (Leap Year)' actually part of the file data?  Is there 
some place in the world where 366 days is *not* a leap year?


> ....OR....
> This may be (as shown below) between the current date and the last
> day of the year or with missing data 331,
> 332,
> 333,
> ...
> 365,
> 366, (Leap Year)
> ----------------------------------
> I would like to create a new file: ofiln based on the data in
> ifiln...  Days without values (missing data or future date) would be
> set as null. If this is run on November 2, 2007 then all days between
> November 3, 2007 and December 31, 2007 would have a date and a "null
> value" for parameter value.

That seems easy enough.


> Passed in as commandline arguments
> arg1 (ex: 201)
> arg2 (ex: 11)
> startdate (2007) - January 1, 2007
> The data would be incremented as per column one value of ifiln.

What exactly do arg1 and arg2 represent?  Which data would be 
incremented?


> The output file should look something like this:
>         insert some text1, 366, insert some text2 201, insert some
> text3 11, 4144.62, some text4, 2007/11/20, some text5

Where did the date 2007/11/20 come from?


> below is what I have... can someone assist...  Thanks
>
> *** I do NOT know how to increment (and format as "2007/01/01") date
> so that it crosses over for each new month and know when it is a leap
> year. Also, Is they a better way to write this??
>
> *------------------------
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $ifiln = 'abc.csv';
> my $ofiln = 'xyz.txt';
>
> # get start year (yyyy) from command line
> my($rsvr, $rdt, $dyr) = @ARGV;

What exactly do $rsvr and $rdt represent?


> print "Year input argument: $ARGV[0] - $ARGV[1] - $ARGV[2]\n";
> print "Year input: $dyr, $rsvr, $rdt\n";
>
> my(@data_line, @rsvrs);       # start out empty

@data_line should be declared inside the loop.


> my $line = "default text";    # start with some default text

$line should be declared inside the loop.


> my $cntr = 0;         # line cntr

$cntr is not needed.


> open RDATA, $ifiln or die "can not open file: $ifiln: $!\n";
>
> while($line = <RDATA>)

You should declare $line here:

while ( my $line = <RDATA> )


> {
> @data_line = split(/,/,$line);

You should declare @data_line here:

    my @data_line = split( /,/, $line );


> $rsvrs[$cntr]{jday} = $data_line[0];
> $rsvrs[$cntr]{rval} = $data_line[1];
> $cntr++;

You don't need $cntr, just use push():

    push @rsvrs, { jday => $data_line[ 0 ], rval => $data_line[ 1 ] };


> }
>
> close(RDATA);
>
> $cntr = 0;

Again, $cntr is not needed.


> open(ORDATA, ">$ofiln");

You should *always* verify that the file opened correctly:

open ORDATA, '>', $ofiln or die "Cannot open '$ofiln' $!";


> foreach(@rsvrs)
> {
>       print ORDATA "insert some text1,";
>       print ORDATA $rsvrs[$cntr]{jday};

You are looping over each element of @rsvrs so in every iteration the 
value of $_ is a reference to a hash and you can just dereference it:

        print ORDATA $_->{ jday };


>       print ORDATA "insert some text2 $rsvr,";
>       print ORDATA "insert some text3 $rdt,";
>       print ORDATA chomp($rsvrs[$cntr]{rval});

chomp() returns the number of $/ values that were removed from its 
argument(s) so that is not what you want to do here.  You should 
probably have chomped the data when you input it.

As above, you can dereference the hash directly:

        print ORDATA $_->{ rval };


>       print ORDATA "some text4";
>       print ORDATA "Calendar Date 2007/11/20 format - How? ($dyr)";

Calendar Date from where?


>       print ORDATA "some text5\n";
>     $cntr++;
> }
>
> close(ORDATA);



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to