Home > Archive > PERL Beginners > November 2007 > Need help with date-time and output
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Need help with date-time and output
|
|
| Gerald Wheeler 2007-11-20, 7:00 pm |
| 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
4,4144.38
5,4144.44
6,4144.48
7,4144.48
8,4144.50
9,4144.50
10,4144.48
11,4144.50
12,4144.52
13,4144.52
14,4144.54
15,4144.54
16,4144.56
17,4144.58
18,4144.58
19,4144.60
20,4144.62
......
365,4147.42
366,4147.56 (Leap Year)
.....OR....
This may be (as shown below) between the current date and the last day of =
the year or with missing data=20
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.
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.=20
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
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?? =20
*------------------------
#!/usr/bin/perl
use strict;
use warnings;
my $ifiln =3D 'abc.csv';
my $ofiln =3D 'xyz.txt';
# get start year (yyyy) from command line
my($rsvr, $rdt, $dyr) =3D @ARGV;
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
my $line =3D "default text"; # start with some default text
my $cntr =3D 0; # line cntr
open RDATA, $ifiln or die "can not open file: $ifiln: $!\n";
while($line =3D <RDATA> )
{
@data_line =3D split(/,/,$line);
$rsvrs[$cntr]{jday} =3D $data_line[0];
$rsvrs[$cntr]{rval} =3D $data_line[1];
$cntr++;
}
close(RDATA);
$cntr =3D 0;
open(ORDATA, ">$ofiln");
foreach(@rsvrs)
{
print ORDATA "insert some text1,";
print ORDATA $rsvrs[$cntr]{jday};
print ORDATA "insert some text2 $rsvr,";
print ORDATA "insert some text3 $rdt,";
print ORDATA chomp($rsvrs[$cntr]{rval});
print ORDATA "some text4";
print ORDATA "Calendar Date 2007/11/20 format - How? ($dyr)";
print ORDATA "some text5\n";
$cntr++;
}
close(ORDATA);
| |
| Tom Phoenix 2007-11-20, 7:00 pm |
| On 11/20/07, Gerald Wheeler <GWHEELER@uc.usbr.gov> 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 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.
That's a non-trivial problem. Have you seen the date and time modules
available on CPAN? You can download and install a module that will
help you with your task.
http://search.cpan.org
Cheers!
--Tom Phoenix
Stonehenge Perl Training
| |
| John W . Krahn 2007-11-20, 7:00 pm |
| 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
| |
| Gunnar Hjalmarsson 2007-11-20, 7:00 pm |
| Tom Phoenix wrote:
> On 11/20/07, Gerald Wheeler <GWHEELER@uc.usbr.gov> wrote:
>
> That's a non-trivial problem.
Not too complicated either. This piece of code may or may not help the
OP move forward:
use Time::Local;
sub nextday {
my ($y, $m, $d) = split /\//, shift;
my $t = timegm 0, 0, 0, $d, $m-1, $y;
($d, $m, $y) = ( gmtime($t+24*60*60) )[3..5];
sprintf '%d/%02d/%02d', $y+1900, $m+1, $d;
}
print nextday('2007/11/20');
Also, Gerald, did you ever answer the questions that Tom asked you in
http://www.mail-archive.com/beginne...g/msg90026.html ?
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
| |
| Tom Phoenix 2007-11-20, 7:00 pm |
| On 11/20/07, Eric Krause <eric987@comcast.net> wrote:
> I think he can't download modules.
I think he *thinks* he can't download modules.
Cheers!
--Tom Phoenix
Stonehenge Perl Training
| |
| Gerald Wheeler 2007-11-21, 10:01 pm |
| First off thanks for the assistance
See inline comments:
On Tuesday 20 November 2007 14:00, Gerald Wheeler wrote:[color=darkred]
> 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?
stating the issues
[color=darkred]
> ....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.
[color=darkred]
> 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?
arg1, arg2 (arguments 1, 2) are simply the first two values passed in
from the commandline: 201 and 11 (these are constants and are not
incremented. Just need to be inserted into each line of output.)
> 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?
inserted based on the year value (2007) input via the commandline entry:
2007
Thus: The ouput file will contain
....some text and values... 2007/01/01.... some text
....some text and values... 2007/01/02.... some text
....some text and values... 2007/01/03.... some text
....some text and values... 2007/01/04.... some text
....some text and values... 2007/01/05.... some text
....
....some text and values... 2007/02/01.... some text
....some text and values... 2007/02/02.... some text
....
thru the end of the calendar year (versus fiscal year, water year,
whatever year)
records with a date in the future (today is November 21, 2007 so all
records (one/day) through December 31 will have null/blank data values
but will still have a calendar date entry as such:
....some text and values... 2007/12/30.... some text
....some text and values... 2007/12/31.... some text
[color=darkred]
> 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?
[color=darkred]
> 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);
[color=darkred]
punch days and all!
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
| |
| Gerald Wheeler 2007-11-21, 10:01 pm |
| John,
I have modified my script as per your suggestions:
**
++
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.
++
I am attempting to remove the newline character at the end of the input =
line so that during output there wil be no newline character (control) to =
start a new line where I do not want one.
I have enter chomp($line); in various places but I get errors each time. =
Where should I chomp "$line" at in the script?
print ORDATA "Calendar Date 2007/11/20 format - How? ($dyr)";
Calendar Date from where?
This (it appears) is a problem:
I would like to output the date in this format: 2007/01/01, etc through =
the end of the calendar year
So, if I pass the script a value (year) of 2007, the first record of the =
output file will contain (plus additional data) the text (date) string: =
2007/01/01, the second record of the output file will contain the string: =
2007/01/02 and so on through the last reord of the file: 2007/12/31 =
(taking into consideration leap year)
Solution or not I would like to thank you for your assistance.
**
*------------------------
#!/usr/bin/perl
use strict;
use warnings;
my $ifiln =3D 'abc.csv';
my $ofiln =3D 'xyz.txt';
# get start year (yyyy) from command line
my($rsvr, $rdt, $dyr) =3D @ARGV;
print "Year input argument: $ARGV[0] - $ARGV[1] - $ARGV[2]\n";
print "Year input: $dyr, $rsvr, $rdt\n";
open RDATA, $ifiln or die "can not open file: $ifiln: $!\n";
while(my $line =3D <RDATA> )
{
my @data_line =3D split( /,/, $line );
push @rsvrs, { jday =3D> $data_line[ 0 ], rval =3D> $data_line[ 1 ] };
}
close(RDATA);
open ORDATA, '>', $ofiln or die "Cannot open '$ofiln' $!";
foreach(@rsvrs)
{
print ORDATA "insert some text1,";
print ORDATA $_->{ jday };
print ORDATA "insert some text2 $rsvr,";
print ORDATA "insert some text3 $rdt,";
print ORDATA $_->{ rval };
print ORDATA "some text4";
print ORDATA "Calendar Date 2007/11/20 format - How? ($dyr)";
print ORDATA "some text5\n";
}
close(ORDATA);
*-------------------------
On Tuesday 20 November 2007 14:00, Gerald Wheeler wrote:[color=darkred]
> 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=20
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=20
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 =3D 'abc.csv';
> my $ofiln =3D 'xyz.txt';
>
> # get start year (yyyy) from command line
> my($rsvr, $rdt, $dyr) =3D @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 =3D "default text"; # start with some default text
$line should be declared inside the loop.
> my $cntr =3D 0; # line cntr
$cntr is not needed.
> open RDATA, $ifiln or die "can not open file: $ifiln: $!\n";
>
> while($line =3D <RDATA> )
You should declare $line here:
while ( my $line =3D <RDATA> )
> {
> @data_line =3D split(/,/,$line);
You should declare @data_line here:
my @data_line =3D split( /,/, $line );
> $rsvrs[$cntr]{jday} =3D $data_line[0];
> $rsvrs[$cntr]{rval} =3D $data_line[1];
> $cntr++;
You don't need $cntr, just use push():
push @rsvrs, { jday =3D> $data_line[ 0 ], rval =3D> $data_line[ 1 ] };
> }
>
> close(RDATA);
>
> $cntr =3D 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=20
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=20
argument(s) so that is not what you want to do here. You should=20
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
--=20
use Perl;
program
fulfillment
--=20
To unsubscribe, e-mail: beginners-unsubscribe@perl.org=20
For additional commands, e-mail: beginners-help@perl.org=20
http://learn.perl.org/=20
| |
| Gerald Wheeler 2007-11-21, 10:01 pm |
| Gunnar,
I checked and find that I do have the Time::Local module installed
The problem is ... how do I increment the day from start to end of the
calendar year?
If i initially set nextday to: 2007/01/01 how do i easily increment
this?
Thanks
Tom Phoenix wrote:[color=darkred]
> On 11/20/07, Gerald Wheeler <GWHEELER@uc.usbr.gov> wrote:
basic[color=darkred]
so[color=darkred]
year.[color=darkred]
>
> That's a non-trivial problem.
Not too complicated either. This piece of code may or may not help the
OP move forward:
use Time::Local;
sub nextday {
my ($y, $m, $d) = split /\//, shift;
my $t = timegm 0, 0, 0, $d, $m-1, $y;
($d, $m, $y) = ( gmtime($t+24*60*60) )[3..5];
sprintf '%d/%02d/%02d', $y+1900, $m+1, $d;
}
print nextday('2007/11/20');
Also, Gerald, did you ever answer the questions that Tom asked you in
http://www.mail-archive.com/beginne...g/msg90026.html ?
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
| |
| Gerald Wheeler 2007-11-21, 10:01 pm |
| I checked and find that I do have the Time::Local module installed
The problem is ... how do I increment the day from start to end of the
calendar year?
If i initially set nextday to: 2007/01/01 how do i easily increment
this?
It appears that Date::Calc would do what I need only I do not have
access to additional modules
Thanks
J.
John,
I have modified my script as per your suggestions:
**
++
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.
++
I am attempting to remove the newline character at the end of the input
line so that during output there wil be no newline character (control)
to start a new line where I do not want one.
I have enter chomp($line); in various places but I get errors each time.
Where should I chomp "$line" at in the script?
print ORDATA "Calendar Date 2007/11/20 format - How? ($dyr)";
Calendar Date from where?
This (it appears) is a problem:
I would like to output the date in this format: 2007/01/01, etc through
the end of the calendar year
So, if I pass the script a value (year) of 2007, the first record of the
output file will contain (plus additional data) the text (date) string:
2007/01/01, the second record of the output file will contain the
string: 2007/01/02 and so on through the last reord of the file:
2007/12/31 (taking into consideration leap year)
Solution or not I would like to thank you for your assistance.
**
*------------------------
#!/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;
print "Year input argument: $ARGV[0] - $ARGV[1] - $ARGV[2]\n";
print "Year input: $dyr, $rsvr, $rdt\n";
open RDATA, $ifiln or die "can not open file: $ifiln: $!\n";
while(my $line = <RDATA> )
{
my @data_line = split( /,/, $line );
push @rsvrs, { jday => $data_line[ 0 ], rval => $data_line[ 1 ] };
}
close(RDATA);
open ORDATA, '>', $ofiln or die "Cannot open '$ofiln' $!";
foreach(@rsvrs)
{
print ORDATA "insert some text1,";
print ORDATA $_->{ jday };
print ORDATA "insert some text2 $rsvr,";
print ORDATA "insert some text3 $rdt,";
print ORDATA $_->{ rval };
print ORDATA "some text4";
print ORDATA "Calendar Date 2007/11/20 format - How? ($dyr)";
print ORDATA "some text5\n";
}
close(ORDATA);
*-------------------------
[color=darkred]
On Tuesday 20 November 2007 14:00, Gerald Wheeler wrote:[color=darkred]
> 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: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
| |
| Tom Phoenix 2007-11-21, 10:01 pm |
| On 11/21/07, Gerald Wheeler <gwheeler@uc.usbr.gov> wrote:
> It appears that Date::Calc would do what I need only I do not have
> access to additional modules
You don't need to be the system administrator to install modules. See
the perlmodinstall manpage.
http://perldoc.perl.org/perlmodinstall.html
Good luck with it!
--Tom Phoenix
Stonehenge Perl Training
| |
| Gunnar Hjalmarsson 2007-11-21, 10:01 pm |
| Gerald Wheeler wrote:
> Gunnar Hjalmarsson wrote:
>
> I checked and find that I do have the Time::Local module installed
You should have, since it's a standard module that comes with Perl.
> The problem is ... how do I increment the day from start to end of the
> calendar year?
>
> If i initially set nextday to: 2007/01/01 how do i easily increment
> this?
You don't _set_ nextday to anything. You _pass_ a date, e.g.
'2007/01/01', to the function nextday(), which _returns_ the next date.
This is an example use of the function:
my $date = '2007/01/01';
while ( substr($date, 0, 4) eq '2007' ) {
print "$date\n";
$date = nextday($date);
}
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
| |
| John W . Krahn 2007-11-22, 4:01 am |
| On Wednesday 21 November 2007 09:40, Gerald Wheeler wrote:
> John,
> I have modified my script as per your suggestions:
>
>
> #!/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;
>
> print "Year input argument: $ARGV[0] - $ARGV[1] - $ARGV[2]\n";
> print "Year input: $dyr, $rsvr, $rdt\n";
>
> open RDATA, $ifiln or die "can not open file: $ifiln: $!\n";
> while(my $line = <RDATA> )
> {
> my @data_line = split( /,/, $line );
> push @rsvrs, { jday => $data_line[ 0 ], rval => $data_line[ 1 ] };
> }
>
> close(RDATA);
>
> open ORDATA, '>', $ofiln or die "Cannot open '$ofiln' $!";
> foreach(@rsvrs)
> {
> print ORDATA "insert some text1,";
> print ORDATA $_->{ jday };
>
> print ORDATA "insert some text2 $rsvr,";
> print ORDATA "insert some text3 $rdt,";
>
> print ORDATA $_->{ rval };
>
> print ORDATA "some text4";
> print ORDATA "Calendar Date 2007/11/20 format - How? ($dyr)";
>
> print ORDATA "some text5\n";
> }
>
> close(ORDATA);
It looks like you may need something like this:
#!/usr/bin/perl
use strict;
use warnings;
use Time::Local;
my $ifiln = 'abc.csv';
my $ofiln = 'xyz.txt';
# get start year (yyyy) from command line
@ARGV == 3 or die "usage: $0 rsvr rdt dyr\n";
my ( $rsvr, $rdt, $dyr ) = @ARGV;
print "Year input argument: $ARGV[0] - $ARGV[1] - $ARGV[2]\n";
print "Year input: $dyr, $rsvr, $rdt\n";
open RDATA, '<', $ifiln or die "can not open file: $ifiln: $!\n";
my @rsvrs;
while ( my $line = <RDATA> )
{
chomp $line;
my @data_line = split /,/, $line;
$rsvrs[ $data_line[ 0 ] ] = $data_line[ 1 ];
}
close RDATA;
open ORDATA, '>', $ofiln or die "Cannot open '$ofiln' $!";
my $current_doy = ( localtime )[ 7 ];
foreach ( 1 .. $#rsvrs )
{
my ( $d, $m, $y ) = ( localtime timelocal 0, 0, 12, $_, 0, $dyr )[
3, 4, 5 ];
my $date = sprintf '%04d/%02d/%02d', $y + 1900, $m + 1, $d;
print ORDATA
"insert some text1,",
$_,
"insert some text2 $rsvr,",
"insert some text3 $rdt,",
defined $rsvrs[ $_ ] ? $current_doy > $_ ? $rsvrs[ $_ ] :
'NULL' : 'NULL',
"some text4",
"Calendar Date $date ($dyr)",
"some text5\n";
}
close ORDATA;
John
--
use Perl;
program
fulfillment
| |
| Gerald Wheeler 2007-11-22, 7:00 pm |
| Thanks I'll try in..
J.
On Wednesday 21 November 2007 09:40, Gerald Wheeler wrote:[color=darkred]
> John,
> I have modified my script as per your suggestions:
>
>
> #!/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;
>
> print "Year input argument: $ARGV[0] - $ARGV[1] - $ARGV[2]\n";
> print "Year input: $dyr, $rsvr, $rdt\n";
>
> open RDATA, $ifiln or die "can not open file: $ifiln: $!\n";
> while(my $line = <RDATA> )
> {
> my @data_line = split( /,/, $line );
> push @rsvrs, { jday => $data_line[ 0 ], rval => $data_line[ 1 ] };
> }
>
> close(RDATA);
>
> open ORDATA, '>', $ofiln or die "Cannot open '$ofiln' $!";
> foreach(@rsvrs)
> {
> print ORDATA "insert some text1,";
> print ORDATA $_->{ jday };
>
> print ORDATA "insert some text2 $rsvr,";
> print ORDATA "insert some text3 $rdt,";
>
> print ORDATA $_->{ rval };
>
> print ORDATA "some text4";
> print ORDATA "Calendar Date 2007/11/20 format - How? ($dyr)";
>
> print ORDATA "some text5\n";
> }
>
> close(ORDATA);
It looks like you may need something like this:
#!/usr/bin/perl
use strict;
use warnings;
use Time::Local;
my $ifiln = 'abc.csv';
my $ofiln = 'xyz.txt';
# get start year (yyyy) from command line
@ARGV == 3 or die "usage: $0 rsvr rdt dyr\n";
my ( $rsvr, $rdt, $dyr ) = @ARGV;
print "Year input argument: $ARGV[0] - $ARGV[1] - $ARGV[2]\n";
print "Year input: $dyr, $rsvr, $rdt\n";
open RDATA, '<', $ifiln or die "can not open file: $ifiln: $!\n";
my @rsvrs;
while ( my $line = <RDATA> )
{
chomp $line;
my @data_line = split /,/, $line;
$rsvrs[ $data_line[ 0 ] ] = $data_line[ 1 ];
}
close RDATA;
open ORDATA, '>', $ofiln or die "Cannot open '$ofiln' $!";
my $current_doy = ( localtime )[ 7 ];
foreach ( 1 .. $#rsvrs )
{
my ( $d, $m, $y ) = ( localtime timelocal 0, 0, 12, $_, 0, $dyr )[
3, 4, 5 ];
my $date = sprintf '%04d/%02d/%02d', $y + 1900, $m + 1, $d;
print ORDATA
"insert some text1,",
$_,
"insert some text2 $rsvr,",
"insert some text3 $rdt,",
defined $rsvrs[ $_ ] ? $current_doy > $_ ? $rsvrs[ $_ ] :
'NULL' : 'NULL',
"some text4",
"Calendar Date $date ($dyr)",
"some text5\n";
}
close ORDATA;
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
| |
| Gerald Wheeler 2007-11-26, 10:02 pm |
| I am getting and error: Day '32" out of range 1..31 at ... line 35
for this line: my ( $d, $m, $y ) = ( localtime timelocal 0, 0, 12,
$_, 0, $dyr )[ 3, 4, 5 ];
and I don't quite know why.. The value for "$_" should be reset to 1
after Jan 31st..
Output is created through only January 31
Thanks
J.
On Wednesday 21 November 2007 09:40, Gerald Wheeler wrote:[color=darkred]
> John,
> I have modified my script as per your suggestions:
>
>
> #!/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;
>
> print "Year input argument: $ARGV[0] - $ARGV[1] - $ARGV[2]\n";
> print "Year input: $dyr, $rsvr, $rdt\n";
>
> open RDATA, $ifiln or die "can not open file: $ifiln: $!\n";
> while(my $line = <RDATA> )
> {
> my @data_line = split( /,/, $line );
> push @rsvrs, { jday => $data_line[ 0 ], rval => $data_line[ 1 ]
};
> }
>
> close(RDATA);
>
> open ORDATA, '>', $ofiln or die "Cannot open '$ofiln' $!";
> foreach(@rsvrs)
> {
> print ORDATA "insert some text1,";
> print ORDATA $_->{ jday };
>
> print ORDATA "insert some text2 $rsvr,";
> print ORDATA "insert some text3 $rdt,";
>
> print ORDATA $_->{ rval };
>
> print ORDATA "some text4";
> print ORDATA "Calendar Date 2007/11/20 format - How? ($dyr)";
>
> print ORDATA "some text5\n";
> }
>
> close(ORDATA);
It looks like you may need something like this:
#!/usr/bin/perl
use strict;
use warnings;
use Time::Local;
my $ifiln = 'abc.csv';
my $ofiln = 'xyz.txt';
# get start year (yyyy) from command line
@ARGV == 3 or die "usage: $0 rsvr rdt dyr\n";
my ( $rsvr, $rdt, $dyr ) = @ARGV;
print "Year input argument: $ARGV[0] - $ARGV[1] - $ARGV[2]\n";
print "Year input: $dyr, $rsvr, $rdt\n";
open RDATA, '<', $ifiln or die "can not open file: $ifiln: $!\n";
my @rsvrs;
while ( my $line = <RDATA> )
{
chomp $line;
my @data_line = split /,/, $line;
$rsvrs[ $data_line[ 0 ] ] = $data_line[ 1 ];
}
close RDATA;
open ORDATA, '>', $ofiln or die "Cannot open '$ofiln' $!";
my $current_doy = ( localtime )[ 7 ];
foreach ( 1 .. $#rsvrs )
{
my ( $d, $m, $y ) = ( localtime timelocal 0, 0, 12, $_, 0, $dyr )[
3, 4, 5 ];
my $date = sprintf '%04d/%02d/%02d', $y + 1900, $m + 1, $d;
print ORDATA
"insert some text1,",
$_,
"insert some text2 $rsvr,",
"insert some text3 $rdt,",
defined $rsvrs[ $_ ] ? $current_doy > $_ ? $rsvrs[ $_ ] :
'NULL' : 'NULL',
"some text4",
"Calendar Date $date ($dyr)",
"some text5\n";
}
close ORDATA;
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
| |
| Gerald Wheeler 2007-11-26, 10:02 pm |
| One additional note:
The input data, column one has values 1 to 365(6)
Thanks
J.
I am getting and error: Day '32" out of range 1..31 at ... line 35
for this line: my ( $d, $m, $y ) = ( localtime timelocal 0, 0, 12,
$_, 0, $dyr )[ 3, 4, 5 ];
and I don't quite know why.. The value for "$_" should be reset to 1
after Jan 31st..
Output is created through only January 31
Thanks
J.
[color=darkred]
On Wednesday 21 November 2007 09:40, Gerald Wheeler wrote:[color=darkred]
> John,
> I have modified my script as per your suggestions:
>
>
> #!/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;
>
> print "Year input argument: $ARGV[0] - $ARGV[1] - $ARGV[2]\n";
> print "Year input: $dyr, $rsvr, $rdt\n";
>
> open RDATA, $ifiln or die "can not open file: $ifiln: $!\n";
> while(my $line = <RDATA> )
> {
> my @data_line = split( /,/, $line );
> push @rsvrs, { jday => $data_line[ 0 ], rval => $data_line[ 1 ]
};
> }
>
> close(RDATA);
>
> open ORDATA, '>', $ofiln or die "Cannot open '$ofiln' $!";
> foreach(@rsvrs)
> {
> print ORDATA "insert some text1,";
> print ORDATA $_->{ jday };
>
> print ORDATA "insert some text2 $rsvr,";
> print ORDATA "insert some text3 $rdt,";
>
> print ORDATA $_->{ rval };
>
> print ORDATA "some text4";
> print ORDATA "Calendar Date 2007/11/20 format - How? ($dyr)";
>
> print ORDATA "some text5\n";
> }
>
> close(ORDATA);
It looks like you may need something like this:
#!/usr/bin/perl
use strict;
use warnings;
use Time::Local;
my $ifiln = 'abc.csv';
my $ofiln = 'xyz.txt';
# get start year (yyyy) from command line
@ARGV == 3 or die "usage: $0 rsvr rdt dyr\n";
my ( $rsvr, $rdt, $dyr ) = @ARGV;
print "Year input argument: $ARGV[0] - $ARGV[1] - $ARGV[2]\n";
print "Year input: $dyr, $rsvr, $rdt\n";
open RDATA, '<', $ifiln or die "can not open file: $ifiln: $!\n";
my @rsvrs;
while ( my $line = <RDATA> )
{
chomp $line;
my @data_line = split /,/, $line;
$rsvrs[ $data_line[ 0 ] ] = $data_line[ 1 ];
}
close RDATA;
open ORDATA, '>', $ofiln or die "Cannot open '$ofiln' $!";
my $current_doy = ( localtime )[ 7 ];
foreach ( 1 .. $#rsvrs )
{
my ( $d, $m, $y ) = ( localtime timelocal 0, 0, 12, $_, 0, $dyr )[
3, 4, 5 ];
my $date = sprintf '%04d/%02d/%02d', $y + 1900, $m + 1, $d;
print ORDATA
"insert some text1,",
$_,
"insert some text2 $rsvr,",
"insert some text3 $rdt,",
defined $rsvrs[ $_ ] ? $current_doy > $_ ? $rsvrs[ $_ ] :
'NULL' : 'NULL',
"some text4",
"Calendar Date $date ($dyr)",
"some text5\n";
}
close ORDATA;
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
|
|
|
|
|