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();
  • Comment on How to read through form input line by line and categorize the information
  • Download Code

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
    Please, use the <code>...</code> tags for the sample input, too, so we can see where the line breaks are. Your code kind of works for me: it prints the lines before the html tag. Inside the HTML, @query is empty, because you already shifted everything from it in the loop.
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
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

    The code snippet below demonstrates two techniques that you might use here. One, if you have data in a string ($user_input) you can open a file handle to that string by providing a reference to the string instead of a filename. Two, if you set $/ to the empty string, you can read from a file handle in paragraph mode, i.e. <$file_handle> reads chunks separated by blank lines.

    use strict; use warnings; use Data::Dumper; my $user_input = <<EOI; first para some text second para more text EOI local $/ = ''; open my $string_handle, '<', \$user_input; my @para = <$string_handle>; close $string_handle; chomp @para; print Dumper \@para;

    If you then read the file handle in list mode (my @para = <$string_handle>;) you get all you want in one go. What you need to be careful about is that your manipulation of $/ does not affect other code, so usually one would use local to avoid that problem.