Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
seprochnik
committed
Oct 17, 2012
1 parent
96f2bc8
commit 892b939
Showing
3 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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! |