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