bluegospel has asked for the wisdom of the Perl Monks concerning the following question:
I've tried to determine why this script reads what's supposed to be several array elements from the beginning of a text file, making them instead a single string, as a single element of the array. This seems to be the case because when the array is later reversed, joined as a string and printed, I get the foremost part of the text file at the end in its original order, then the last several entries of the text file reversed at the beginning. I've isolated the getArray() subroutine as the culprit since it appears everything following it is doing what it's supposed to. Input is just user comments on a publication. I'll include the form below this script:
#!/usr/bin/perl #commentsPublisher.pl use 5.8.8; use warnings; use strict; use CGI; my $comments=new CGI; #------- my @comments=accessor(); setText(); my @text=getArray(); my $flipped=flipText(@text); setHtml($flipped); #first, define the subroutines sub accessor{ my $name=$comments->param('name'); my $whichDaily=$comments->param('whichDaily'); my $comments1=$comments->param('comments'); my $quickResponse; if ($comments->param('quickResponse')){ $quickResponse="[" . $comments->param('quickResponse'). "]"; }else{ $quickResponse="[no quick-select]"; } @comments=($name, $whichDaily, $quickResponse, $comments1); } sub setText{ my $commentsString=join ("\n", @comments); open my $writeComments, '>>', '/comments.txt'; $commentsString=$commentsString . "\n----------\n"; print $writeComments $commentsString; close $writeComments; } sub getArray{ #you want to return an array from here open my $readComments, '/comments.txt'; my @text1=<$readComments>; close $readComments; my $text=join ('', @text1); #at this point you should have a string, $text, comprised of #your text file, commentsTesting.txt as a string, including #\n's & ----- @text1=split(/----------\n/, $text); return @text1; #so you've put each line into each element of the array, #preserving your newline chars & incl \n's & ---------- #you've translated to a string, $text, via join, incl \n's & ----- #you've translated back into array, (split by -----), where #each section is an element; note ----- are excluded } sub flipText{ my @text1=@_; my @flipped=reverse(@text1); my $flipped1=join ("----------\n", @flipped); $flipped1=$flipped1 . "----------\n"; return $flipped1; } sub setHtml{ my $flipped1=shift; $flipped1=~s/\n/\n<br>/g; my $preText=qq{<body bgcolor="E6E6FA"><a href="/commentsPublisher. +html"> <h3>Please click here to post your comments.</h3></a><br>\n }; open my $setHtml, '>', '/comments.html'; print $setHtml "$preText $flipped1 </body>"; close $setHtml; } #------- print $comments->header(); print $comments->start_html("Published"); print "<br><br><center>Thank You!<br><br>\n\n"; print qq{<a href="/cgi-bin/comments.html">Click here to view comments< +/a><br><br> or use your browser's back button to post something new.} +; print $comments->end_html(); 1;
Here's the html form:
<br><br> <body bgcolor="E6E6FA"> <center> <table bgcolor="B0C4DE" border="1" align="center" border="5"> <tr><td colspan="2"> <center>Visitors' Comments</center><br> </td></tr> <form action="/cgi-bin/commentsPublisher.pl" method="post" name="Reade +r Comments"> <tr><td colspan="2">Name: <input name="name" type="text" size="20" max +length="20"></td> </tr> <tr><td colspan="2"> Which "Daily" <br> are you commenting on?:<br> <textarea name="whichDaily" cols="20" rows="2"></textarea><br><br> </td></tr> <tr><td colspan="2">Quick-select here:</td></tr> <tr> <td> <input type="radio" name="quickResponse" value="inspiring"> Inspiring </td> <td> <input type="radio" name="quickResponse" value="interesting"> Interest +ing </td> </tr> <tr> <td> <input type="radio" name="quickResponse" value="toodeep"> Too Deep </td> <td> <input type="radio" name="quickResponse" value="unclear"> Not very cle +ar </td> </tr> <tr> <td> <input type="radio" name="quickResponse" value="boring"> Boring </td> <td> <input type="radio" name="quickResponse" value="challenging"> Challeng +ing </td> </tr> <tr> <td> <input type="radio" name="quickResponse" value="irritating"> Irritatin +g </td> <td> <input type="radio" name="quickResponse" value="infuriating"> Infuriat +ing </td> </tr> <tr><td colspan="2">And/or write your own comment here:</td></tr> <tr><td colspan="2"> <textarea name="comments" cols="25" rows="7"></textarea><br><br> </td></tr> <tr> <td colspan="2"><center><input type="submit" value="Post Comments"></c +enter> </td> </tr> </form> </table> </center> </body>
Help is appreciated.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: array gone amiss
by jethro (Monsignor) on Oct 13, 2010 at 21:03 UTC |