Please wrap computer text (code, data, output) in <c>...</c> tags. It'll even handle escaping for you.

@selectfields needs to hold two values per record, the id and the value of the selected column.

sub text_to_html { my $s = shift; $s =~ s/&/&amp;/g; $s =~ s/</&lt;/g; $s =~ s/"/&quot;/g; return $s; } sub build_select_field_from_db { # -------------------------------------------------------- # Builds a SELECT field from the database. my ($column, $value, $name) = @_; $name ||= $column; my $fieldnum; for my $i (0..$#db_cols) { if ($column eq $db_cols[$i]) { $fieldnum = $i; last; } } if (!defined($fieldnum)) { return "error building select field: no fields specified!"; } open (my $db_fh, "<", $db_file_name) or &cgierr("unable to open $db_file_name. Reason: $!"); my @lines = <$db_fh>; close $db_fh; my @selectfields; foreach my $line (@lines) { next if $line =~ /^#/; # Skip comment lines. next if $line =~ /^\s*$/; # Skip Blank Lines. chomp ($line); my @fields = &split_decode ($line); my ($row_id, $row_value) = @fields[0, $fieldnum]; if (!(grep $_[1] eq $fields[$fieldnum], @selectfields)) { push @selectfields, [ $fields[0], $fields[$fieldnum] ]; } } my $output = qq|<SELECT NAME="$name">|; $output .= "<OPTION>---"; foreach (sort { $a->[1] cmp $b->[1] } @selectfields) { my ($row_id, $row_value) = @$_; my $esc_id = text_to_html($row_id); my $esc_value = text_to_html($row_value); my $selected = $value eq $row_value ? ' selected' : ''; $output .= qq{<option$selected value="$esc_id">$esc_value}; } $output .= "</SELECT>"; return $output; }

Update: That grep bothers me.

sub build_select_field_from_db { # -------------------------------------------------------- # Builds a SELECT field from the database. my ($column, $value, $name) = @_; $name ||= $column; my $fieldnum; for my $i (0..$#db_cols) { if ($column eq $db_cols[$i]) { $fieldnum = $i; last; } } if (!defined($fieldnum)) { return "error building select field: no fields specified!"; } open (my $db_fh, "<", $db_file_name) or &cgierr("unable to open $db_file_name. Reason: $!"); my @lines = <$db_fh>; close $db_fh; my %selectfields; foreach my $line (@lines) { next if $line =~ /^#/; # Skip comment lines. next if $line =~ /^\s*$/; # Skip Blank Lines. chomp ($line); my @fields = &split_decode ($line); my ($row_id, $row_value) = @fields[0, $fieldnum]; $selectfields{$row_value} = $row_id; } my $output = qq|<SELECT NAME="$name">|; $output .= "<OPTION>---"; foreach my $row_value (sort keys (%selectfields)) { my $row_id = $selectfields{$row_value}; my $esc_id = text_to_html($row_id); my $esc_value = text_to_html($row_value); my $selected = $value eq $row_value ? ' selected' : ''; $output .= qq{<option$selected value="$esc_id">$esc_value}; } $output .= "</SELECT>"; return $output; }

In reply to Re: Help syntax? by ikegami
in thread Help syntax? by randallmarquis

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.