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

I have a form that sends information to an action page. It works great but was hoping get distinct field information. Here is what it is doing now where the first field has two values and the second field has three values:
First: Oregon First: Neveda Second: California Second: Texas Second: Colorado
I need it to look like this where the field will print just once when it has multiple values:
First: Oregon Neveda Second: California Texas Colorado
Here is my form entries on the html page:
<SELECT NAME="Data_One" Size="6" MULTIPLE> <OPTION>Oregon</OPTION> <OPTION>Neveda</OPTION> <OPTION>Idaho</OPTION> </SELECT> <SELECT NAME="Second_Part" Size="6" MULTIPLE> <OPTION>California</OPTION> <OPTION>Texas</OPTION> <OPTION>Colorado</OPTION> </SELECT>
My attempt on the Perl action page:
foreach $field (param) { foreach $value (param($field)) { if($field =~ /Data_One/) { $field = "First"; } if($field =~ /Second_Part/) { $field = "Second"; } if($value !~ /^\s*$/) { $allData .= "$field\:\n$value\n\n"; } } } ..... <table align=\"center\" width=520> <tr><td><pre>$allData</pre></td><tr> </table>
Please advise.

Replies are listed 'Best First'.
Re: Distinct field name output attempt
by polettix (Vicar) on May 16, 2005 at 14:17 UTC
    Just accumulate, then print.
    foreach $field (param) { # Isolate non-empty values my @values = grep { ! /^\s*$/ } param($field); next unless @values; # Skip if nothing available # Establish a header line. Note that I added an "Other" class my $header = "Other:"; $header = "First:" if $field =~ /Data_One/; $header = "Second:" if $field eq /Second_Part/; # Print it all. $, is the output record separator for prints local $, = "\n"; print $header, @values, "\n"; }
    Untested, but you get the idea. Note that localising $, inside the foreach isn't a very smart idea for performance issues, you'd better localising it slightly outside the loop if you want to gain some CPU cycles.

    Moreover, if you're only interested into these two selection boxes, you'd better iterate over them directly:

    foreach $field (qw(Data_One Second_Part)) { # Isolate non-empty values my @values = grep { ! /^\s*$/ } param($field); next unless @values; # Skip if nothing available # Establish a header line. my $header = "First:"; $header = "Second:" if $field =~ /Second_Part/; # Print it all. $, is the output record separator for prints local $, = "\n"; print $header, @values, "\n"; }

    Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

    Don't fool yourself.
Re: Distinct field name output attempt
by holli (Abbot) on May 16, 2005 at 14:26 UTC
    Unless I am missing something this should do it:
    print "First:\n"; print "$_\n" for grep { /[^\s]/ } param("Data_One"); print "Second:\n"; print "$_\n" for grep { /[^\s]/ } param("Second_Part");
    Note: In the grep I only test if the value contains something different than spaces. That logic is flipped to your code, but more natural for grep. It should do the same.


    holli, /regexed monk/
      Thanks to all of you!
Re: Distinct field name output attempt
by mrborisguy (Hermit) on May 16, 2005 at 13:54 UTC
    what have you tried so far?
    to get you going, i think i'd probably start with a:
    my $last_field = "";
    at the top of the script somewhere. then test if $last_field eq $field, then don't print $field again, just print the value. you're gonna have to set $last_field = $field somewhere in there too.