Thursday, January 27, 2011

Zodiac.pl

Today while discussing about our Site MD sending a CNY greeting, we joked about him using a template and substituting the year and its respective zodiac sign. YH suggested it the zodiac might be a $ variable.


The $ variable is a common variable in perl. So I went and developed a simple script that allows the user to check whether that particular year is of which zodiac animal.

###############################################################################

#!/usr/bin/perl -w

print " what year ? : \n" ;

$year = <STDIN> ;
chomp $year ;

@animals2 = qw ( monkey rooster dog pig rat ox tiger rabbit dragon snake horse ram ) ;
$animals2 = @animals2 ;

$index = $year % 12 ;


print " $year is the year of the $animals2[$index]" ;


###############################################################################

The script uses the modulus operator , % to get the remainder of each division of the year against 12.
The index , $index is used to select which element of the array @animals2 to pick to determine what year is of what animal.


Notice the @animals2 array has been tweaked to adapt to our indexing counter $index.




No comments:

Post a Comment