Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

If expression with parameter or variable read! Help!

Status
Not open for further replies.

mheilbut

Programmer
Apr 24, 2002
53
BR
I have a perl program which is now reading one of two possibilities:
1- The program reads two variables (ARGV[1] and ARGV[2]) which are dates that a insert as parameters
ex: ./toped.pl 01/05/2002 to 31/05/2002(date in Brazil is different)

2- The seconde option is it reads a variable defined previosly without parameters:
ex:
#@t = localtime();
#$tmon = $t[4] + 1;
#$tmon = &quot;0&quot; . $tmon if ($tmon < 10);
#$tyear = $t[5] + 1900;
#$firstday = join(&quot;-&quot;, &quot;01&quot;, $tmon, $tyear);
my $ini = &quot;'&quot;.$firstday.&quot;'&quot;;

My question is the following:
How can I make an if statement formula such that it reads the parameter if I put any, and if I don't, it reads the formula ($firstday)? Michel, São Paulo, Brazil
 
One way would be to check the number of arguments '$#ARGV'. Here's some sample code:
Code:
if ($#ARGV < 0) {
    @t = localtime();
    $tmon = $t[4] + 1;
    $tmon = &quot;0&quot; . $tmon if ($tmon < 10);
    $tyear = $t[5] + 1900;
    $firstday = join(&quot;-&quot;, &quot;01&quot;, $tmon, $tyear); 
} else {
    $firstday = $ARGV[0];
    $firstday =~ s|/|-|g;
}
my $ini    = &quot;'&quot;.$firstday.&quot;'&quot;;
 
If you do:

$foo = shift;

to get the arguments, then you can do the test:

if ($foo eq &quot;&quot;)

to see if any input parameter was used.
 
$firstday = shift || join(&quot;-&quot;, &quot;01&quot;, $tmon, $tyear);

jaa
 
I tried using raider2001 idea but I didnt get it to work.
I actually have two variables:

(First day of month)
@t = localtime();
$tmon = $t[4] + 1;
$tmon = &quot;0&quot; . $tmon if ($tmon < 10);
$tyear = $t[5] + 1900;
$firstday = join(&quot;-&quot;, &quot;01&quot;, $tmon, $tyear);

(Yesterday´s date)
$time = time();
@y = localtime($time - 86400); # 1 day is 86400 seconds
$ymon = $y[4] + 1;
$ymon = &quot;0&quot; . $ymon if ($ymon < 10);
$ymday = $y[3];
$ymday = &quot;0&quot; . $ymday if ($ymday < 10);
$yyear = $y[5] + 1900;
$ydate = join(&quot;-&quot;, $ymday, $ymon, $yyear);

This is how I get the variable:
my $repres = $ARGV[0];
my $ini = &quot;'&quot;.$firstday.&quot;'&quot;;
or &quot;'&quot;.$ARGV[1].&quot;'&quot;;
my $fim = &quot;'&quot;.$ydate.&quot;'&quot;;
or &quot;'&quot;.$ARGV[2].&quot;'&quot;;
(OR I get the variable or the parameter)

I need an if expression for this.
Thanks. Michel, São Paulo, Brazil
 
ANYONE PLEASE!!
I really need this to work!
Thankx! Michel, São Paulo, Brazil
 
Michel,

Does this do what you want?
Code:
if ($#ARGV == 0) {
    # Get first day of month
    @t = localtime();
    $tmon = $t[4] + 1;
    $tmon = &quot;0&quot; . $tmon if ($tmon < 10);
    $tyear = $t[5] + 1900;
    $firstday = join(&quot;-&quot;, &quot;01&quot;, $tmon, $tyear); 
    # Get yesterday's date
    $time = time;
    @y = localtime($time - 86400); # 1 day is 86400 seconds
    $ymon = $y[4] + 1;
    $ymon = &quot;0&quot; . $ymon if ($ymon < 10);
    $ymday = $y[3];
    $ymday = &quot;0&quot; . $ymday if ($ymday < 10);
    $yyear = $y[5] + 1900;
    $ydate = join(&quot;-&quot;, $ymday, $ymon, $yyear);
} elsif ($#ARGV == 2) {
    # Use command line arguments
    $firstday = $ARGV[1];
    $firstday =~ s|/|-|g;
    $ydate = $ARGV[2];
    $ydate =~ s|/|-|g;
} else {
    print &quot;ERROR: Argument error \n&quot;;
    exit(1);
}
my $ini    = &quot;'&quot;.$firstday.&quot;'&quot;;
my $fim    = &quot;'&quot;.$ydate.&quot;'&quot;;
print &quot;$ini $fim \n&quot;;;
 
Ok, if you want to do it that way:

$ini = join &quot;&quot;, &quot;'&quot;,$ARGV[1] || $firstday,&quot;'&quot;;
$fim = join &quot;&quot;, &quot;'&quot;,$ARGV[2] || &quot;TEST&quot;, &quot;'&quot;;

jaa
 
I get the following message using what you proposed:
Use of uninitialized value in concatenation (.) at /usr/progs/pedrep.pl line 86. Michel, São Paulo, Brazil
 
Um, are you talking about raider2001's code or mine?

if you are using mine, these lines

$ini = join &quot;&quot;, &quot;'&quot;,$ARGV[1] || $firstday,&quot;'&quot;;
$fim = join &quot;&quot;, &quot;'&quot;,$ARGV[2] || &quot;TEST&quot;, &quot;'&quot;;

have NO concat operators (.) only comma operators (,)

jaa
 
To Justice41:
Where would I put
$ini = join &quot;&quot;, &quot;'&quot;,$ARGV[1] || $firstday,&quot;'&quot;;
$fim = join &quot;&quot;, &quot;'&quot;,$ARGV[2] || &quot;TEST&quot;, &quot;'&quot;;
in my program?
What part of the program would this fit in.
Would I have to read $firstday before using my formula? Michel, São Paulo, Brazil
 
Something like:

#(First day of month)
@t = localtime();
$tmon = ++$t[4] < 10 ? &quot;0&quot;.$t[4] : $t[4];
$tyear = $t[5] + 1900;
$firstday = join(&quot;-&quot;, &quot;01&quot;, $tmon, $tyear);

#(Yesterday´s date)
$time = time();
@y = localtime($time - 86400); # 1 day is 86400 seconds
$ymon = ++$t[4] < 10 ? &quot;0&quot;.$t[4] : $t[4];
$ymday = $t[3] < 10 ? &quot;0&quot;.$t[3] : $t[3];
$yyear = $y[5] + 1900;
$ydate = join(&quot;-&quot;, $ymday, $ymon, $yyear);

my $repres = $ARGV[0] || &quot;default ?&quot;;
my $ini = join &quot;&quot;, &quot;'&quot;,$ARGV[1] || $firstday,&quot;'&quot;;
my $fim = join &quot;&quot;, &quot;'&quot;,$ARGV[2] || &quot;$ydate&quot;, &quot;'&quot;;

print &quot;I: $ini\n&quot;;
print &quot;F: $fim\n&quot;;

This isn't the most efficient way because it calculates the two dates even if they were specified on the command line. raider2001's way is more efficient in this respect.

jaa
 
Aaah! There are errors in my previous post.
I used @t instead of @y in the yesterday calc.
This is correct:

#!/usr/bin/perl -w

#(First day of month)
@t = localtime();
$tmon = ++$t[4] < 10 ? &quot;0&quot;.$t[4] : $t[4];
$tyear = $t[5] + 1900;
$firstday = join(&quot;-&quot;, &quot;01&quot;, $tmon, $tyear);

#(Yesterday´s date)
$time = time();
@y = localtime($time - 86400); # 1 day is 86400 seconds
$ymon = ++$y[4] < 10 ? &quot;0&quot;.$y[4] : $y[4];
$ymday = $y[3] < 10 ? &quot;0&quot;.$y[3] : $y[3];
$yyear = $y[5] + 1900;
$ydate = join(&quot;-&quot;, $ymday, $ymon, $yyear);

my $repres = $ARGV[0] || &quot;default ?&quot;;
my $ini = join &quot;&quot;, &quot;'&quot;,$ARGV[1] || $firstday,&quot;'&quot;;
my $fim = join &quot;&quot;, &quot;'&quot;,$ARGV[2] || &quot;$ydate&quot;, &quot;'&quot;;

print &quot;I: $ini\n&quot;;
print &quot;F: $fim\n&quot;;

jaa
 
Thanks, it worked!
I have one more question though when I put the following before getting the $firstday in order to check if it is the first day of the month, I get an error.
Can anyone tell me why?

#today's date
$tday = $t[3];
#if today is the first day of the month
if ($tday eq &quot;01&quot;)
{
#and if the month is January
if ($tmon eq &quot;01&quot;)
{
# make the month December
$tmon = 12;
}
else
{
#subtract 1 from the variable that contains the month
$tmon--;
}
} Michel, São Paulo, Brazil
 
This is because on the first day of the month $tday == &quot;1&quot; not &quot;01&quot;. You need to change the test in the if statement or zero pad the day like with the month.

$tday = $t[3] < 10 ? &quot;0&quot;.$t[3] : $t[3];

jaa
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top