LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Last monday of each month Perl (https://www.linuxquestions.org/questions/programming-9/last-monday-of-each-month-perl-901621/)

jt1 09-07-2011 11:58 AM

Last monday of each month Perl
 
Hello LQ,

I am new to Perl scripting and have completed a Perl script. The final part of the script needs to find the last Monday of each month. So if it is August, the script returns 20110829. Does anyone have any idea how to accomplish this via Perl?

Thanks for any help.

paulsm4 09-07-2011 12:55 PM

I'm not aware of any "ready-made" way to do it.

Suggestion: write your own function that will:

1. Get specified day/month date value of the last day of the month (e.g. "sep 30 2011")

2. Get day-of-week of that date value

3. Subtract backwards to the preceding Monday

4. Return that (monday's) date value

Sergei Steshenko 09-07-2011 01:47 PM

Quote:

Originally Posted by jt1 (Post 4464031)
Hello LQ,

I am new to Perl scripting and have completed a Perl script. The final part of the script needs to find the last Monday of each month. So if it is August, the script returns 20110829. Does anyone have any idea how to accomplish this via Perl?

Thanks for any help.

Start from http://datetime.perl.org/wiki/datetime/dashboard .

rkambli 09-07-2011 02:42 PM

With thousands of Perl Modules available I am sure you can find something similar in good datetime manipulation module.

Assuming you are using Linux if you cal call one liner shell script from perl here it is:
Just tested for Sep-11 and Oct-11.


Code:

$ cal -m 9 2011 | cut -c1-2 | sed "/^$/d" | tail -n 1
26
$ cal -m 10 2011 | cut -c1-2 | sed "/^$/d" | tail -n 1
31

$perl -e "system('cal -m 10 2011 | cut -c1-2 | sed "/^$/d" | tail -n 1')"


paulsm4 09-07-2011 03:16 PM

Actually, Sergei Steshenko made an *excellent* suggestion. I'd encourage you to check out the date/time manipulation tools available at the link he gave you!

IMHO .. pSM


All times are GMT -5. The time now is 09:40 AM.