06 January 2010

learning perl - 2

#!/usr/bin/perl
#./test2.perl

use warnings;
use strict;
my $i = 0;

foreach my $i (10..20) {
print "$i ";
}
print "\n";

$i = 9;
while ($i++ < 20) {
print "$i ";
}
print "\n";

$i = 10;
do {
print "$i ";
} while ($i++ < 20);
print "\n";

$i = 9;
until ($i++ == 20) {
print "$i ";
}
print "\n";


$i = 9;
while(1) {
last if $i++ == 21;
next if $i == 20;
print "$i ";
}
print "\n";

my @array = (10..20);
foreach (@array) {
print "$_ ";
$_ *= 2;
}
print "\n@array\n";


my $total = 0;
$total += $_ for @array;
print "total: $total\n";

output: ./test2.perl
10 11 12 13 14 15 16 17 18 19 20
10 11 12 13 14 15 16 17 18 19 20
10 11 12 13 14 15 16 17 18 19 20
10 11 12 13 14 15 16 17 18 19 20
10 11 12 13 14 15 16 17 18 19 21
10 11 12 13 14 15 16 17 18 19 20
20 22 24 26 28 30 32 34 36 38 40
total: 330

No comments:

Post a Comment