1 #!/usr/local/bin/perl
2 open(INFILE, "snowball.txt");
3 print "Content-type:text/html\n\n";
4 print " HTML HEAD \n TITLE Snowball sentence - 22 words /TITLE \n" .
5 " /HEAD \n BODY BGCOLOR=FFFF55 \n H1 PERL and snowballs /H1 " .
6 "\n HR NOSHADE \n";
7 $sentence = "";
8 print "\n H3 FONT COLOR=BLUE \n";
9 while()
10 {
11 print $_;
12 chomp;
13 tr/,;.'"//d;
14 $sentence .= $_;
15 }
16 print "\n /FONT /H3 \n";
17 print "\n P \n";
18 # @words = split (/ /, $sentence);
19 print "\n PRE \n";
20 print join( "\n", split (/ /, $sentence) ); # <-- replaces 5 lines!!
21 # print join("\n", @words); <--Used to be: foreach $w (@words)
22 # {
23 # print "$w\n"; }
23.1 # }
24 print " /PRE \n /BODY \n /HTML \n";
25 close (INFILE);
Note: The 5 lines that the one line
print join( "\n", split (/ /, $sentence) );
replaces are:
1 $words = split (/ /, $sentence);
2 foreach $w (@words)
3 {
4 print "$w\n";
5 }
or the 7 lines solution
1 $words = split (/ /, $sentence);
2 $str = "";
3 foreach $w (@words)
4 {
5 $str = $str . $w . "\n";
6 }
7 print $str;
that
-------------------------------------------
print join( "\n", split (/ /, $sentence) );
-------------------------------------------
replaces,
shows the power of PERL and the spirit of
TIMTOWTDI TIM There Is More
TOW Than One Way
TDI To Do It
There Is More Than One Way To Do It
T I M T O W T D I
The group exercise during class #7 on Tuesday, June 19th
was to replace the 5 lines (or 7 lines version) to fewer lines
and eliminate the need for a foreach loop by using the join
function. ----
The LESS THAN and GREATER THAN TAG CHARACTERS had to be
removed from the above PERL script, because Internet
Explorer insists on processing them instead of treating
them as PRE formatted text.