Richard Lee wrote:
I just read FAQ on finding out yesterday's time.

I see that one of the easy way to find out is

my $date = scalar localtime( ( time() - ( 24 * 60 * 60 ) ) );
print "$date\n";

and it works fine for me

I also see lot of modules that will make life easier for beginners.. but since I was learning reference, I wanted to try this on my own
since Leap year is not a huge factor, so

my @Jan = (1..31);
my @Feb = (1..29);
my @Mar = (1..31);
my @Apr = (1..30);
my @May = (1..31);
my @Jun = (1..30);
my @Jul = (1..31);
my @Aug = (1..31);
my @Sep = (1..30);
my @Oct = (1..31);
my @Nov = (1..30);
my @Dec = (1..31);

my @cal_r = [ @Jan,@Feb,@Mar,@Apr,@May,@Jun,@Jul,@Aug,@Sep,@Oct,@Nov,@Dec ];

for my $i ( 0 .. $#cal_r ) {
   for my $j ( 0 .. $#{ $cal_r[ $i ] } ) {
          print "$cal_r[ $i ][ $j ]\n";
   }
}

and then just check to see if it's first day of the month and if it is, go through @cal_r and find the previous month and find the last array item.. Algorithm seems to be correct but I cannot get to the item the way I thought I could.

What would be the correct syntax if I knew the date? say Apr 1st to find the yesterday's date given above?

I was thinking something along the line of

for my $i ( 0 .. $#cal_r ) {
   if ($cal_r[$i][0]) {
         print "yesterday is $cal_r[$i-1][lastday]\n"
   }
}


I rewrote it like this(not considering all possiblities) for now.

#!/usr/bin/perl -w

use strict;
use Data::Dumper;

my %cal;
my @Month = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);

$cal{'Jan'} = [ 1..31 ];
$cal{'Feb'} = [ 1..29 ];
$cal{'Mar'} = [ 1..31 ];
$cal{'Apr'} = [ 1..30 ];
$cal{'May'} = [ 1..31 ];
$cal{'Jun'} = [ 1..30 ];
$cal{'Jul'} = [ 1..31 ];
$cal{'Aug'} = [ 1..31 ];
$cal{'Sep'} = [ 1..30 ];
$cal{'Oct'} = [ 1..31 ];
$cal{'Nov'} = [ 1..30 ];
$cal{'Dec'} = [ 1..31 ];

#print "$cal{'Dec'}->[0]\n";

print "Enter a date\n";
chomp(my $answer = <STDIN>);
my ($month, $day) = split(/ /,$answer);

if ($day == '1') {
  if ($month eq 'Jan') {
     print "yesterday was $Month[-1] $cal{ $Month[-1] }[-1]\n";
  } else {
      my $pos = '0';
      for (@Month) {
        if ( $month eq $_ ) {
           last;
        } else {
            ++$pos;
        }
       }
print "yesterday was $Month[$pos - 1 ] $cal{ $Month[ $pos - 1] }[-1]\n";
  }
} else {
  my $c_day = $day - 1;
  print "yesterday was $month $c_day \n";
}


--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to