diff --git a/review_session_scripts/factorial.pl b/review_session_scripts/factorial.pl new file mode 100644 index 0000000..db61c81 --- /dev/null +++ b/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"; + diff --git a/review_session_scripts/head.pl b/review_session_scripts/head.pl new file mode 100644 index 0000000..175ba95 --- /dev/null +++ b/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"; +} diff --git a/review_session_scripts/text.txt b/review_session_scripts/text.txt new file mode 100644 index 0000000..87c6dc3 --- /dev/null +++ b/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!