Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Looping months and days

by Anonymous Monk
on Jun 18, 2001 at 23:27 UTC ( [id://89411]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello...

My name is Seema, I am new on this site..please bare with me.

I have looked at a few of the questions and examples, in some of the responses that other people have had problems with. Though I didn't find one that would relate to my problem.

I would like to ask you a question. I am working on a project which deals with Unix, Perl, and HTML scripts. I have already created a website which will monitor on a day-to-day basis (with the Unix machine).

Though, I have ran into a slight problem. In order for this website to work, I need to write a script in Perl so I can put it on the Unix machine. I need a script that will loop 31 days, daily.

For example: April 30 - May 30. The loop needs to repeat daily. So, when its May 31, the dates should change to May 1. April 30 should be dropped off the loop.

My question is...how would I come about doing this particular loop? Hopefully someone can suggest a few example scripts or references which can help me out.

Thanks! ~Seema



Edit 2001-06-18 -- ar0n Fixed formatting.
Edit 2001-06-18 -- Masem Changed title from "Perl Script"

Replies are listed 'Best First'.
Re: Perl Script
by TGI (Parson) on Jun 19, 2001 at 00:08 UTC

    Please read this site's FAQ for info on how to post. Please check the info on formatting your post. Your post is hard to read.

    I'm not sure exactly what you are asking for, but it sounds like you want code that do something based on the last 31 days each day. Even if I am on the wrong track, checking out these functions should help you:

    1. time
    2. localtime
    3. Time::Local's timelocal

    Now for some (untested) code.

    #Loop 31 times foreach (1..31) { # Set date to $_ days in the past. my $date = time() - $_*24*60*60 my ($day,$month,$year) = localtime($date)[3,4,5]; # cleanup raw localtime output $month++; $year = substr ($year, -2); # Print niceish (whitespace instead of 0) MM/DD/YY dates to string $date = sprintf "%2d/%2d/%2d", $month,$day,$year; #I'm reusing $da +te, I don't need its old value. # Fix whitespace. $date will now hold MM/DD/YY data. $date=~s/\s/0/g; print "MM/DD/YY:\t$date\n"; }

    Here's a truncated bit of output:

    MM/DD/YY: 06/17/01 MM/DD/YY: 06/16/01 MM/DD/YY: 06/15/01 MM/DD/YY: 06/14/01

    I hope this helps.


    TGI says moo

Re: Perl Script
by Mission (Hermit) on Jun 18, 2001 at 23:43 UTC
    Seema, I don't want to be rude, but I'm a bit confused about what you are asking. Do you have some code written already? If so, please post it between the <code> tags. Is this looping something that the server runs, CRON like? What is the purpose of this looping? Sometimes the solution you are looking for isn't the answer to the problem.

    I think this is an issue about looping over dates, which should be easy to resolve. However since I don't know what your purpose is, I suggest that you use our 'Search' at the top of each page, or try the Perl Monks powerful Super Search. I also suggest that you look through our Tutorials. There is lots of information about basic looping there. Finally, I suggest that you register an account with Perl Monks and post code, ask questions, learn, and participate in our community. BTW: Welcome.

    - Mission
    "Heck I don't know how to do it either, but do you think that's going to stop me?!!"
Re: Perl Script
by earthboundmisfit (Chaplain) on Jun 19, 2001 at 00:58 UTC

    It seems to me the easiet way to deal with this is to use a Julian days scheme. Use the search utility --> 'Julian days' The results point to many different CPAN modules for conversion back and forth.

    You would simply add and subtract 31 from today's Julian day and convert to normal dates to get your range. It's unclear what you want to do with the range, so that's all I'll say.

Re: Perl Script UH, What?
by Nitsuj (Hermit) on Jun 19, 2001 at 00:40 UTC
    Ok, I'm not sure what your program is doing, but it would seem that for some reason you need to record info based on the date... or perhaps a perpetual calendar.

    I think that the most obvious suggestion for this is to search CPAN to see if there is a pm written to do this for you. You seem to want to record something on a day to day basis and then output a summary of the past month's information or something?

    You can create a simple data structure to handle this and code to perform the summative functions over it.

    One potential way to do this is to make a database hash (thus saving the information to a file) based on the date, and create the summative information over a months worth of info. I'm not going to write out code for you to do this as it sounds like you're getting paid for this, and I'm not, and since realistically speaking, I only have a vague idea of what you are trying to do.

    Just Another Perl Backpacker
Re: Perl Script
by hsweet (Pilgrim) on Jun 19, 2001 at 03:46 UTC
    I don't know if this is the right answer for what you want to do, but you can write a unix crontab file that will run your script once a day whenever you want. The dates you can parse out of localtime. It's not clear from your question what you want the script to do, other than get the date. If that's all you need, this should do it. The line below is a copy of a crontab file I use on my site to run a perl script that indexes my site every night for a search engine. 30 2 * * * /home/httpd/cgi-bin/perlfect/search/indexer.pl Ignorance with Confidence
Re: Perl Script
by Anonymous Monk on Jun 19, 2001 at 01:41 UTC
    I think the best choice for this would be the module Date::Calc as this will allow you to calculate dates as you require, allowing for leap years etc...
      using a mix of all the above Date::Calc, and Time::Local should enable you to knock something together very simply and quickly. All are available for download from the CPAN
Re: Looping months and days
by Anonymous Monk on Jun 22, 2001 at 18:03 UTC
    In unix their is a utlity i.e crontab Importance of this utility is,you can set it to daily jobs Path /etc/crontab * * * * * path of the script sec minu hours days years I hope that it will fit for your application

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://89411]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-25 19:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found