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

Hi peeps

Am just getting into hashes etc. and getting confused. After doing my homework, I know I need to use $query->checkbox_group 'cos I need the value different from the 'on-screen' label (using an image).

I've checked out the docs and examples at CPAN, but they 'assume' an associative array has already been created for the following example;

$query->checkbox_group(-name=>'group_name', -values=>['eenie','meenie','minie','moe'], -default=>['eenie','moe'], -linebreak=>'true', -labels=>\%labels );
What I'd like to know is how to create the data which \%labels contains? All I'll be doing is iterating through an array of variable names to build up a list of .... well, have a look for yourself:
foreach $key (0..$#languages){ $flag = ${'flag_' . $lang[$key][1]}; }

What can I do to get the necessary info into \%labels so I can use it for the $query->checkbox_group example?!?!?!?

Thanks in advance
Richard.

Replies are listed 'Best First'.
Re: CGI & Creating an associative array dynamically
by dragonchild (Archbishop) on May 20, 2004 at 12:00 UTC
    I have no idea where to start on this answer.

    Ok. let's start with the obvious ones:

    1. You need to put use strict; at the top of every single Perl file you work with.
    2. You most especially do NOT want to use soft references. ${'flag_' . $x} is a symbolic reference. There are numerous posts on this site as to why not.
    3. A hash is built as so:
      my %labels; foreach my $lang (@languages) { $labels{"flag_$lang->[1]"} = "Something here"; }
    4. Invest the US$20 and buy yourself a copy of Learning Perl which will explain all of this for you.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

    I shouldn't have to say this, but any code, unless otherwise stated, is untested

Re: CGI & Creating an associative array dynamically
by Zaxo (Archbishop) on May 20, 2004 at 12:04 UTC

    You can create a reference to an anonymous hash with braces {}.

    $query->checkbox_group( # . . . -labels => { map { $_ => ucfirst } qw/eenie meenie minie moe/} );
    You can do that outside the function call as well. That would let you use the array of field names in the label hash definition.

    After Compline,
    Zaxo