Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Problem assigning values to Hash

by Steny (Sexton)
on May 31, 2004 at 18:22 UTC ( #357822=perlquestion: print w/ replies, xml ) Need Help??
Steny has asked for the wisdom of the Perl Monks concerning the following question:

I've recently discovered Data::Dumper (I love it with all my heart, and don't know how I got through the last few years without it) but can't for the life of me seem to figure out why:
my $userInfo; my $number = 1; my $temp = 1; push @$userInfo, { number => $number, name => param("n$i"), id => param("id$i"), rollSizzle => param("rs$i"), roll => $temp, sizzle => $temp} ; print Dumper($userInfo);
is returning the following with Data::Dumper:
$VAR1 = [ { '' => '', '1' => undef, 'rollSizzle' => 'roll', 'id' => 't +est', 'name' => 'test', 'number' => 1 } ];
I may just be going nutz (or blanking out, due to staring at this screen for too long), but can anyone see why 'roll' and 'sizzle' aren't being assigned the value of '1'... instead of messing up the hash?

Any insight/help would be greatly appreciated :-)


Steny

Comment on Problem assigning values to Hash
Select or Download Code
Re: Problem assigning values to Hash
by tinita (Parson) on May 31, 2004 at 18:40 UTC
    param('key') returns an empty list (in list context) in case it is empty.
    replace all param()-calls with scalar param(...), then you'll see that some param()s are undef (update: or, as kvale said, they have multiple values).
Re: Problem assigning values to Hash
by kvale (Monsignor) on May 31, 2004 at 18:40 UTC
    It looks like that in one of the pairs, 1 is being treated as key, not a value. Perhaps the param subroutine is returning multiple elements and confusing the ordering.

    By contrast, the following works just fine on my system:

    use Data::Dumper; my $userInfo; my $number = 1; my $temp = 1; push @$userInfo, { number => $number, name => 'param', id => 'param', rollSizzle => 'param', roll => $temp, sizzle => $temp} ; print Dumper($userInfo);

    -Mark

Re: Problem assigning values to Hash
by blokhead (Monsignor) on May 31, 2004 at 18:41 UTC
    First of all, my congratulations for discovering Data::Dumper, you will never go anywhere without it ;)

    This is a common problem with CGI's param method. It's being called in list context in this case. If the CGI form did not include a value for rs$i, then param("rs$i") in list context will return an empty list. The empty list just gets flattened away in a hash construction:

    { number => 1, name => "test", id => "test", rollSizzle => (), roll => undef, sizzle => undef } ## is the same as { number => 1, name => "test", id => "test", rollSizzle => "roll", undef => "sizzle", undef }
    This explains the output you're getting. You can fix it by forcing scalar context:
    { number => $number, name => scalar param("n$i"), id => scalar param("id$i"), rollSizzle => scalar param("rs$i"), roll => $temp, sizzle => $temp };
    At some point, this problem has bitten everyone who's ever done CGI programming with CGI.pm, and I wish there were an option to pass to CGI.pm to force scalar context behavior for all calls to param. I rarely use forms where multiple fields are named the same, so param's list context is rarely useful for me...

    Now that I think of it, I will probably start changing the tops of my CGI scripts from now on:

    # use CGI 'param'; use CGI; sub param { scalar CGI::param(@_) }

    blokhead

      Finally got it working. Thx a ton, guys! I really appreciate it.
Re: Problem assigning values to Hash
by TomDLux (Vicar) on Jun 01, 2004 at 00:51 UTC

    You would have warning messages if you used

    use warnings; use strict; use diagnostic;

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

Re: Problem assigning values to Hash
by demerphq (Chancellor) on Jun 01, 2004 at 14:36 UTC

    Say scalar(param("foo")) and youll be fine.

    If you like DD, then I suggest you have a look at DDS. Which has alot of enhanced features over DD, and gets much less wrong than DD does. For a speed price admittedly, but if debugging is your objective then DDS will (hopefully) be better.


    ---
    demerphq

      First they ignore you, then they laugh at you, then they fight you, then you win.
      -- Gandhi


Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://357822]
Approved by kvale
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (11)
As of 2014-07-11 12:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (224 votes), past polls