Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Now that you've gotten answers to your question, I figured I'll answer the real question and give an answer that you can't easily read yourself to about data structures.

The first and most interesting part is to redesign the data structure. It seems that you want an ordered list and therefore have an array with the columns, but you also want to have several values attached to that value. My suggestion is that you consider having a data structure looking like

my @data = ( [ Gender => [ 'Male', 'Female'] ], [ A => [ 'a', 'b', 'c'] ], );
which would remove the need for parallel data structures. Your code would then look like
for (@data) { my ($column, $options) = @$_; print $column . ': '; foreach my $option (@$options) { print '<input type="radio" name="' . $column . '" value="' . $ +option . '"> ' . $option . ' '; } }
As a side note, I think that print statement is more clearly written using printf() and another qoute delimiter:
printf qq{<input type="radio" name="%s" value="%s">%s\n}, $column, $option, $option ;
But really, this is unnecessary. CGI already implements a &radio_group subroutine that we can use and get rid of the loop altogether.

The end result is

use strict; use CGI; my $q = CGI::->new; my @data = ( [ Gender => [ 'Male', 'Female'] ], [ A => [ 'a', 'b', 'c'] ], ); for (@data) { my ($column, $options) = @$_; print $q->radio_group( -name => $column, -values => $options, -default => '_', ); # '_' is chosen as default as that will make # no value checked by default, as long as you # don't have '_' as an option. }

If you at any time would like to do a lookup of which values that are accepted for a radio group you can degenerate your @data to %dataoptions by using

my %dataoptions = map @$_, @data;

Related documents:
perlref - Perl references and nested data structures
perlfunc - Perl builtin functions (look for map)
CGI - Simple Common Gateway Interface Class


See perltoc if you don't know which perldoc to read!
Read argumentation in its context!

In reply to Re: Foreach in a 2D array by ihb
in thread Foreach in a 2D array by monoxide

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    Domain Nodelet?
    and the web crawler heard nothing...

    How do I use this?Last hourOther CB clients
    Other Users?
    Others browsing the Monastery: (2)
    As of 2024-07-14 08:29 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found

      erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.