Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Problem assigning values to Hash

by blokhead (Monsignor)
on May 31, 2004 at 18:41 UTC ( #357826=note: print w/ replies, xml ) Need Help??


in reply to Problem assigning values to Hash

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


Comment on Re: Problem assigning values to Hash
Select or Download Code
Replies are listed 'Best First'.
Re: Re: Problem assigning values to Hash
by Steny (Sexton) on May 31, 2004 at 19:22 UTC
    Finally got it working. Thx a ton, guys! I really appreciate it.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (14)
As of 2015-07-30 15:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (273 votes), past polls