Showing posts with label Perl. Show all posts
Showing posts with label Perl. Show all posts

Thursday, May 5, 2011

Perl for the day: while loop construct


use strict;
use warnings;

# to play cricket in my perl program

print "\nPress any of 0 to 9\n\n";

use Term::ReadKey;
my $bat = 1;
my $player;
my $num;
my $team;
my $ran = 0;
my $spot = 0;
my @res;

ReadMode 'cbreak';

while (++$spot < 3) {
    print "Team: ", $spot, "\n";
    $num = 0;
    $team = 0;
    while (++$num < 11) {
        print "    Player ", $num, ": ";
        $player = 0;
        while ($bat != 0) {
            $bat = int(ReadKey(0));
            $ran = int(rand(12345)) + $bat;
            $bat = $ran % 6;
            if ($bat != 0) {
                print $bat, "+";
            }
            $player += $bat;
        }
        print "=", $player, "\n";
        $team += $player;
        $bat = 1;
    }
    print "  Total score = ", $team,"\n";
    $res[$spot] = $team;
}
if ($res[1] > $res[2]) {
    print "Team 1 Won\n";
} elsif ($res[2] > $res[1]) {
    print "Team 2 Won\n";
} else {
    print "Match Tie\n";
}

Tuesday, May 3, 2011

Perl for the day: for loop construct handling array

use strict;
use warnings;

# fibonacci sequence/series using array in Perl

my $i;
my @ser = (0, 1);

for ($i = 2; $i < $ARGV[0]; $i++) {
    $ser[$i] = $ser[$i - 1] + $ser[$i - 2];
}

for ($i = 0; $i < $ARGV[0]; $i++) {
    print $ser[$i]," ";
}

Monday, May 2, 2011

Perl for the day: for loop construct

use strict;
use warnings;

# fibonacci sequence/series in Perl

my $cur = 1;
my $prev = 0;
my $i, my $tmp;

print $prev, " ";

for ($i = 0; $i < $ARGV[0]; $i++) {
    print $cur, " ";
    $tmp = $cur;
    $cur += $prev;
    $prev = $tmp;
}

Monday, April 25, 2011

Perl for the day: Conditional construct

use strict;
use warnings;
if($#ARGV + 1 < 1) {
    print "Vaadaa\n";
}
else {
    print "Podaaa\n";
}

Friday, April 22, 2011

Perl for the day: Assigning variables

use strict;
use warnings;

# scalar

my $number = 72;
my $strin = "kutti";
my $cha = 'a';

# array

my @arr = (34,56,78,90);
my @sarr = ("amm ", "app ", "tha ", "akk ", "mam ", "kul ", "jai ", "all ");
my @carr = ('a','b','c','d');

print $number,"\n";
print $strin,"\n";
print $cha, "\n";

print $arr[3],"\n";
print $sarr[4],"\n";
print $carr[3],"\n";

print @sarr[4, 5, 6],"\n";
print @carr[0, 1, 2, 3],"\n";

Saturday, April 16, 2011

Thursday, April 14, 2011

Perl for the day: Hello World

Download perl for Windows from the following URL and install it.

http://www.activestate.com/activeperl/downloads

-- hello.pl --
use strict;
use warnings;

print("Hello World\n");