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.


In reply to array gone amiss by bluegospel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.