Skip to content

Commit

Permalink
review session 1 scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
seprochnik committed Oct 17, 2012
1 parent 96f2bc8 commit 892b939
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
15 changes: 15 additions & 0 deletions review_session_scripts/factorial.pl
@@ -0,0 +1,15 @@
#!/usr/bin/perl
use strict;
use warnings;
my $n = shift;
my $i = $n; # make a copy of $n and change that so
# $n keeps the original value entered on the command line
my $factorial = 1;
while ($i > 1) {
$factorial *= $i; # same as $factorial = $factorial * $i ;
print "intermediate value $factorial\n";
$i--;
}
# now you can print the value of $n
print "factorial $n = $factorial\n";

54 changes: 54 additions & 0 deletions review_session_scripts/head.pl
@@ -0,0 +1,54 @@
#!/usr/bin/perl
use warnings;
use strict;
# pass filename and number of lines to read from the file on command line

my $arg1 = shift;
my $arg2 = shift;

open(my $fh, "<", $arg1);







print "\n\nWith while loop------------------\n\n";

my $count = 0;
while ( $count < $arg2 ) {
my $line = <$fh>; #read a line from the file
chomp $line;
print "$line\n";
$count++;
}

print "\n\n\n---- with while loop and last ----\n";
close($fh);

open($fh,"<",$arg1);

#simon doesn't recommend this version - extra complexity
$count = 0;
while ( my $line = <$fh>) {
chomp $line;
print "$line\n";
$count++;
if ($count >= $arg2) {
last;
}

}

close($fh);
open($fh,"<",$arg1);

print "\n\n\n--- with for loop -----\n\n";
# start from zero for array indices, string offsets (perl internals)
# start from one if printing lines for humans
for (my $i =0 ; $i < $arg2 ;$i++) {
my $line = <$fh>;
chomp $line;
print "$line\n";
}
15 changes: 15 additions & 0 deletions review_session_scripts/text.txt
@@ -0,0 +1,15 @@
the representation of written language
Text (literary theory), a concept in literary theory
Another name for a literary work
A particular Bible passage, sometimes a single verse or verse fragment
Textbook, a standardized instructional book
in electronic communication and computing
More lines
Will go
something
like this
and we'll still
run
out
probably
Enough!

0 comments on commit 892b939

Please sign in to comment.