JohnTabor has asked for the wisdom of the Perl Monks concerning the following question:
Hi so what I'nm ultimately trying to do here is get user input from a textarea in a form ( method POST) and basically read it line by line and store the lines in a kind of state variable or counter until a blank line is hit and the value of the state variable is pushed onto an array as one element. The state variable is then reset after this and the process continues until all the input is read. In visual terms, this is what it is supposed to do. Example of user input: some example user input to show something with hello world lorem ipsum * I want the program to read those two first lines and push them into one array element when the program sees that there is a blank line beneath them. Same thing applies with the line "hello world lorem ipsum". This is what I have so far. I had tried a foreach loop at first but it didn't work either
#!/usr/bin/perl use strict; use warnings; use CGI; my $CGI = new CGI(); sub main { print $CGI->header(); my @query = $CGI->param('query'); while(@query) { my $line = shift(@query); print "$line"; } #my $count = 0; #my @res; #foreach my $line(@query) { # if($line =~ /[A-Za-z0-9]/) { # push(@res, $line); # } elsif ($line =~ /^\s$/) { # $count++; # } #} print<<HTML; <html> <head> <style> textarea { resize: none; } </style> </head> <form action="rand.cgi" method="post"> <textarea name="query"></textarea> <input type="submit"/> </form> <p>Last submitted:<br><br><pre>@query</pre></p> </html> HTML } main();
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to read through form input line by line and categorize the information
by choroba (Cardinal) on Jan 19, 2014 at 18:30 UTC | |
|
Re: How to read through form input line by line and categorize the information
by hdb (Monsignor) on Jan 19, 2014 at 18:33 UTC |