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]," ";
}

No comments:

Post a Comment