For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > June 2004 > Re: 2 queries- first day of month and reading zipped files directly off server









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 Re: 2 queries- first day of month and reading zipped files directly off server
Shalini Joshi

2004-06-25, 6:48 pm

Hey.

This is the code I came up with. I am just looking for the first day
of the month, but I still had to use the heavy Date::Manip module.
THis is what I came up with. I was working on a linux system with perl
5.0.8(or some such).It has the Date::Manip module inbuilt but doesnt
have hte Date::Calc one!


use Date::Manip;


#use Date::Calc qw(:all);
my @records;
my $i;
my $wanted_handle;
my @selected_records;
my $date;
my $cpday;
my $cpmonth;
my $cpyear;
my $cpdate;
my $cpfilename;
my %parameters;
my $year;
my $month;
my $day;
my $today;

local $[=1;

open RECORDS, "< AETNA1.txt " or die "Can't read file: $!";

$today=&ParseDate("today");
print "Today's date is $today\n";
$day= &UnixDate($today,"%d");
$month=&UnixDate($today,"%m");
$year=&UnixDate($today,"%Y");

print "$day\n";
print "$month\n";
print "$year\n";

if ($day == 01)
{
print "Today is the first day of the month!!\n";
print "We are going to read the Account Position File!\n";


}

else
{
print "Today is not the first day of the month!!\n";
print "We will make do with the Daily Activity File!\n";
};



That was indeed straightforward!

Thanks for all the help again.

--Shalini
Jeff 'japhy' Pinyan

2004-06-25, 6:48 pm

[posted & mailed]

On 23 Jun 2004, Shalini Joshi wrote:

>local $[=1;


WHOA. What on EARTH did you do THAT for?

>$today=&ParseDate("today");
>print "Today's date is $today\n";
>$day= &UnixDate($today,"%d");
>$month=&UnixDate($today,"%m");
>$year=&UnixDate($today,"%Y");
>
>print "$day\n";
>print "$month\n";
>print "$year\n";
>
>if ($day == 01)


That's a whole lot of work. Determining if today is the first of the
month is a far simpler task, and doesn't require a module.

if ( (localtime)[3] == 1 ) {
# today is the first of the month
}

perldoc -f localtime

--
Jeff Pinyan RPI Acacia Brother #734 RPI Acacia Corp Secretary
"And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)

Shalini Joshi

2004-06-25, 6:48 pm

Hey!

Thanks for the date-pointer...it was a biig help.

Paul Lalli <mr_itty@gmail.com> wrote in message news:<20040623125654.Y23512@dishwasher.cs.rpi.edu>...
> On Wed, 23 Jun 2004, Shalini Joshi wrote:
>
>
> No, you don't have to. You *can*, but you don't have to. As someone else
> in this thread pointed out, if all you want is the date of the month, the
> built in (and far less heavy) localtime() function could be all you need.
>
>
> Most likely 5.8.0 - run `perl -v` to verify.
>
>
> Yes, Date::Manip is a standard module.
>
>
> Date::Calc is available from CPAN: www.cpan.org
>
>
> I'm going to guess that you use all these somewhere else in your actual
> code? If not, please get rid of them. Declaring variables that you don't
> use is bad coding style.


Yeah right I use them elsewhere in the code. Actually i have to name
the output file based on the date read in from the data file. Hence
these variables.

>



>
> Why are you changing the first index of arrays, and first indices of
> substrings? Who (or what documentation) told you to do this, and for what
> purpose?
>


I wanted to parse a really long record....about 160 characters and the
information for parsing that I had started the counting from 1. I
looked up the camel book and to make it less tedious(well!!) for me i
just decided to play with that one, instead of doing the subtractions
!!!




>
> You generally don't want to call subroutines by prefixing them with the &,
> even if you occasionally see it in documentation. It causes side affects
> that are almost always unwanted.
>
>


Thanks a lot for this one..am new and didnt know that. :) Infact your
post has been a biig help.

>
> this works, of course, but just to be picky, you don't need to specify 1
> as 01. If you were doing a string comparison, you would indeed have to
> do:
> if ($day eq '01')
> but you were comparing numerically.
>
>
> As mentioned earlier, if you really just need the day of the month, you
> can skip all that parseDate stuff and just make a call to localtime in
> list context, and take the fourth element from it:
>
> my $day = (localtime)[3];
>
> perldoc -f localtime
> will help you understand what localtime returns.
>
> Hope this helps,


Thanks a bunch..:)

> Paul Lalli
>


Regards,

Shalini
Ben Morrow

2004-06-25, 6:48 pm


Quoth shalinij1@yahoo.com (Shalini Joshi):
> Paul Lalli <mr_itty@gmail.com> wrote in message news:<20040623125654.Y23512@dishwasher.cs.rpi.edu>...
>
> Yeah right I use them elsewhere in the code. Actually i have to name
> the output file based on the date read in from the data file. Hence
> these variables.


It is generally considered bad style in Perl to declare all your
variables at the top of the file. Declare them just before you first use
them, or as near to that as puts them in the correct scope.

>
> I wanted to parse a really long record....about 160 characters and the
> information for parsing that I had started the counting from 1. I
> looked up the camel book and to make it less tedious(well!!) for me i
> just decided to play with that one, instead of doing the subtractions
> !!!


This is a very bad idea: $[ is strongly deprecated, and will be removed
from Perl in the not-too-distant future.

If you have, say, an array of offsets into a string, you can do all the
subtractions at once like this:

@offsets = map { $_ - 1 } @offsets;

or, if you have lots of arrays,

$_-- for @offsets, @more_offsets, $yet, $more, $offsets;

Ben

--
"If a book is worth reading when you are six, * ben@morrow.me.uk
it is worth reading when you are sixty." - C.S.Lewis
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com