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

Hopefully I didn't mess up the title but here is what I am trying to do.
I have a multi-select box in a webpage. When the user hits go the
following is returned in the param:

session=1678f192860fb3850d27244c76b704b1&action=select&salesman=015&sa +lesman=020&salesman=030&salesman=035&salesman=045&salesman=050&salesm +an=065&salesman=075&salesman=090&salesman=095&salesman=100&salesman=1 +05&salesman=110&salesman=120&salesman=900&salesman=911&salesman=998&y +ear=2003&go=GO

I am trying to assign a hash with a key of salesman and the values (array reference)
of 020,030,035,045,etc. I thought this would be simple but am having no luck without
creating two lines one for creating the array and one assigning the array reference to
a hash. I would like to do this in one step.
Any help the the wall I am facing?

Thanks much in advance. Doug

Replies are listed 'Best First'.
•Re: Populating an array reference in a hash with with a regex
by merlyn (Sage) on Oct 01, 2002 at 15:44 UTC
    With the right tools, it's trivial:
    use CGI qw(param); my @salesmen = param('salesman');
    Always use CGI.pm for CGI, unless you have a very compelling counter-reason.

    -- Randal L. Schwartz, Perl hacker

Re: Populating an array reference in a hash with with a regex
by Ovid (Cardinal) on Oct 01, 2002 at 15:49 UTC
    use CGI qw/:standard/; my %data = ( salesman => [param('salesman')] );

    CGI::param() returns a list when in list context.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Populating an array reference in a hash with with a regex
by mephit (Scribe) on Oct 01, 2002 at 16:32 UTC
    I had completely forgotten that the param method worked like that, and I now agree that's what you should use in this instance. In the future, if you have something similar that isn't CGI, or if you're masochistic enough to try to roll your own, this would work:
    my $session = q{1678f192860fb3850d27244c76b704b1&action=select&salesma +n=015&salesman=020&salesman=030&salesman=035&salesman=045&salesman=05 +0&salesman=065&salesman=075&salesman=090&salesman=095&salesman=100&sa +lesman=105&salesman=110&salesman=120&salesman=900&salesman=911&salesm +an=998&year=2003&go=GO}; my %hash; push @{$hash{session}}, $session =~ /salesman=(\d+)/g;
    HTH.

    --

    There are 10 kinds of people -- those that understand binary, and those that don't.

      These are great! I had forgotton that CGI.pm worked that way as well. I was trying to do it as a map and wasn't accomplishing it very well. Thanks again all. Doug
Re: Populating an array reference in a hash with with a regex
by kabel (Chaplain) on Oct 01, 2002 at 15:57 UTC
    this is just for info:
    push @{ $hash{$_->[0]} }, $_->[1] foreach (map {[split /=/]} split (/& +/, $string));
    the obvious disadvantage is that every hash entry is an array reference, but shouldn't matter.