06 January 2010

learning perl - 1

#!/usr/bin/perl
#./test1.perl

use warnings;
use strict;

if ($#ARGV < 0) {
print "usage ./test1.perl <0..11>\n";
exit 0;
}

my $month = $ARGV[0];
my $m;

my @q1 = qw(Jan Feb Mar);
my @q2 = qw(Apr May Jun);
my @q3 = qw(Jul Aug Sep);
my @q4 = qw(Oct Nov Dec);


my @smonth = (@q1, @q2, @q3, @q4);


print "@smonth[$month,5,7..9]\n";
print "$smonth[$month]\n";
print "array size: $#smonth\n";


### print the array
print @smonth,"\n";
print "@smonth\n";

### swap month 0 and 1
@smonth[0,1] = @smonth[1,0];
for $m (@smonth) { print "$m "; }
print "\n";

my @sortedmonth = sort @smonth;
print "@sortedmonth\n";
###
print reverse(@smonth),"\n";

print "@smonth[1..3]\n";

Output: ./test1.perl 3

Mar Jun Aug Sep Oct
Mar
array size: 11
JanFebMarAprMayJunJulAugSepOctNovDec
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
Feb Jan Mar Apr May Jun Jul Aug Sep Oct Nov Dec
Apr Aug Dec Feb Jan Jul Jun Mar May Nov Oct Sep
DecNovOctSepAugJulJunMayAprMarJanFeb
Jan Mar Apr


Reference:
http://www.perl.org/books/beginning-perl

No comments:

Post a Comment