Skip to content

Commit

Permalink
Perl basics
Browse files Browse the repository at this point in the history
  • Loading branch information
sameerz committed Jun 1, 2014
0 parents commit 9b9976b
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
17 changes: 17 additions & 0 deletions HelloWorldPerl/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>HelloWorldPerl</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.epic.perleditor.perlbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.epic.perleditor.perlnature</nature>
</natures>
</projectDescription>
67 changes: 67 additions & 0 deletions HelloWorldPerl/HelloWorld.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#/usr/bin/perl -w

use strict;

print "Hello\n";

my @array=('a','b','c');

print @array;
print "\n";
print "@array\n";

my %x=(a=>1, b=>'asdf', c=>3);

print "%x\n";
print "$x{a}\n";
foreach (sort keys %x) {
print $_;
}
print "\n";
my @array=(1,2,3,4,5,6,7,8,9,10);
print @array, "\n";

sub factorial {
my $x = @_[0];
if($x==0) {
return 1;
}
else {
return $x * factorial ($x - 1)
}
}

my $i;
my @factorial_result;
foreach $i (1..10) {
push @factorial_result, factorial($i);
}

print "factorial: ", join(", ", @factorial_result), "\n";

sub fibonacci {
my $number_of_terms = @_[0];
my $current = 0;
my $prev = 0;
my $next = 1;
my $counter;
my @result_array;
for ($counter = 0; $counter < $number_of_terms; $counter++) {
if($counter <= 1) {
$current = $counter;
}
else {
$current = $prev + $next;
$prev = $next;
$next = $current;
}
#print $current;
push @result_array, $current;
}
return \@result_array;
}

my $fibonacci_result = fibonacci(10);
print "fibonacci: ", join(", ", @{$fibonacci_result});

__END__

0 comments on commit 9b9976b

Please sign in to comment.