bbaily has asked for the wisdom of the Perl Monks concerning the following question:

I have a cgi script that receives input from checkboxes, radio buttons, and textareas. Although I am able to control how the information is stored in the logfile for the radio buttons and the checkboxes (not "multiple"), the textarea is giving me problems. The textarea input can vary from one item to several, I can get the information to print to a log file, like:
HHH9114 EEE8888 LLL4258 PPP8525
but I am trying to make them print like:
||HHH9114||EEE8888||LLL4258||PPP8525||
can you help me please?

Replies are listed 'Best First'.
Re: How do I parse data received from a textarea
by Zaxo (Archbishop) on Jan 17, 2002 at 22:01 UTC

    From a practical standpoint, you cannot rely on users to fill in a textarea in any consistent way. Even if you give clear insteructions and they want to do it right, you'll get some mighty bizarre data. A textarea's data comes back as a single string, so the unforgiving way to parse out an array of values like you show is:

    use strict; use CGI; my $query = new CGI; my @ary = ($query->param('the_textarea')) =~ m/([A-Z]{3}\d{4})/gs; print '||',join('||', @ary),'||';
    You are using CGI.pm aren't you?

    Update: Fixed typo in @ary assignment. Thanks jlongino for sharp eyes and kind acts.

    After Compline,
    Zaxo

Re: How do I parse data received from a textarea
by trs80 (Priest) on Jan 17, 2002 at 22:11 UTC
    If your entries are always seperated by whitespace you can
    do this:
    my $string = qq~HHH9114 EEE8888 LLL4258 PPP8525~; $string =~ s/\s+/|/g; print "||$string||\n";
    That the \s+ is telling it to replace one or more whitespace
    characters (\t\n\r etc.), the g tells it to do all occurances.

Re: How do I parse data received from a textarea
by metadoktor (Hermit) on Jan 17, 2002 at 21:36 UTC
    Presumably you're receiving the data as "foo=HHH9114\nEEE8888\nLLL4258\nPPP8525". You can replace these newlines with double pipes with this code fragment.
    # Put value part in default variable. $_="HHH914\nEEE888\nLLL4258\nPPP8525"; # Search for newlines and replace with double pipes. s/\n/||/g; print "||$_||\n";
    Prints out this:
    ||HHH914||EEE888||LLL4258||PPP8525||

    metadoktor

    "The doktor is in."

      Please note that what is sent for these "newlines" is dependent on what platform the browser is running on:
      \r\n for Windows \n for Unix \r for Macintosh

      Impossible Robot

      I'm not sure line separators differ with TEXTAREA fields no matter what OS is running on the machine with the Browser.

      In my own (less than vast) experience, I have had no problem using code like the following (AFAIK, has worked with Mac-, UNIX- and Windows-based browsers):

      # assuming text is in $_ my @lines = split /\n/; my $text = join '||', @lines; print "||$lines||\n";

      Or, in one line,

      print '||'. join('||', split( /\n/ )) .'||' ."\n";

      dmm

      If you GIVE a man a fish you feed him for a day
      But,
      TEACH him to fish and you feed him for a lifetime
Re: How do I parse data received from a textarea
by Anonymous Monk on Jan 17, 2002 at 23:52 UTC
    Thank you all!!! It worked!!!!