CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
if then else with Date help needed

 



dprogrammer
New User

Mar 22, 2013, 7:58 PM

Post #1 of 6 (556 views)
if then else with Date help needed Can't Post

Dear friends, I am new to Perl. I have simple situation. This is how the current code is and then below is what I am trying to achieve but it's not working:

#Current code:

my $con = 'D:\\MyFiles\con_1.conf';


Here is what I am trying to achive. I now have two .conf files, con_1.conf and con_2.conf. I want them to be assigned to $con variable based upon a condition. If its' first day of the month, then the file should be con_1.conf, else the file should be con_2.conf. Here is what I have but its' not working:

#getting day of month

my $day = $day[( $y + int($y / 4) - int($y / 100) + int($y / 400) + $d[$m - 1] + $d) % 7];

if ($day = 1) {
my $con = 'D:\\MyFiles\con_1.conf';
} else {
my $con = 'D:\\MyFiles\con_2.conf';
}

Any ideas please? I am totally new to Perl.


hwnd
User

Mar 22, 2013, 8:34 PM

Post #2 of 6 (544 views)
Re: [dprogrammer] if then else with Date help needed [In reply to] Can't Post


Code
  

Return a number representing DATE's day of the week from 0 to 6, where 0 means Sunday.

use Date::Simple;

my $date = Date::Simple->new('2013-03-03');
my $when = $date->day_of_week();



Kenosis
User

Mar 22, 2013, 10:42 PM

Post #3 of 6 (542 views)
Re: [dprogrammer] if then else with Date help needed [In reply to] Can't Post

To get the day of the month, you can use the following:


Code
( localtime(time) )[3]


localtime(time) returns a list, detailed here. The third item of the list is the day. Using Perl's ternary operator, consider the following:


Code
#!/usr/bin/env perl 

use strict;
use warnings;

my $con =
( localtime(time) )[3] == 1
? 'D:\\MyFiles\con_1.conf'
: 'D:\\MyFiles\con_2.conf';

print $con;


Output:


Code
D:\MyFiles\con_2.conf


However, it will return:


Code
D:\\MyFiles\con_1.conf


when it's the first day of the month, i.e., when:


Code
( localtime(time) )[3] == 1


is true.

Hope this helps!


(This post was edited by Kenosis on Mar 23, 2013, 9:05 AM)


dprogrammer
New User

Mar 23, 2013, 8:19 AM

Post #4 of 6 (533 views)
Re: [Kenosis] if then else with Date help needed [In reply to] Can't Post

Thanks hwnd and kenosis! I applied Kenosis's solution and it worked perfectly.

Regards,


Kenosis
User

Mar 23, 2013, 9:04 AM

Post #5 of 6 (530 views)
Re: [dprogrammer] if then else with Date help needed [In reply to] Can't Post

You're most welcome, dprogrammer!


dprogrammer
New User

Mar 24, 2013, 10:22 PM

Post #6 of 6 (512 views)
Re: [Kenosis] if then else with Date help needed [In reply to] Can't Post

Ok, now I've slight change in the original code. I need to now check for date and time as well. Like the condition should check first day of the month and time of 07:30AM.
E.g:
localtime(time)==1 07:30AM ? 'D:\\MyFiles\con_1.conf' : 'D:\\MyFiles\con_2.conf';


I am thinking about this but not sure if that's the best approach:

my $con = ( ( localtime(time) ) [1] == 30 && (localtime(time) ) [2] == 7 && (localtime(time) ) [3] == 1 ) ?
'D:\\MyFiles\con_1.conf' : 'D:\\MyFiles\con_2.conf';
print $con;

Any ideas?


(This post was edited by dprogrammer on Mar 25, 2013, 9:45 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives