hash mystery

M

monkeys paw

For some reason i'm forced to use a scalar for
ONE hash element and not the rest. Here is how the
code behaves:

This works:

$qry = new CGI;

foreach (@names) {
if ($_ =~ /^newsummary(.*)$/ && $qry->param($_)) {
# I have no clue why 'id' must be a scalar to work. [sf]
my $id = $qry->param('id' . $1);
%hash = (
file_name => $qry->param('file_name' . $1),
id => $id,
comment_name => $qry->param('newsum_name' . $1),
id_type => 'bill',
commentary => $qry->param($_),
);
}
}

At his point the hash looks like:

file_name => 'file',
id => 'CA2005',
comment_name => 'cname',
id_type => 'bill',
commentary => 'some comment',

However, if i use $qry for 'id' the hash gets whacked like
so:

%hash = (
file_name => $qry->param('file_name' . $1),
id => $qry->param('id' . $1),
comment_name => $qry->param('newsum_name' . $1),
id_type => 'bill',
commentary => $qry->param($_),
);


CA2005 => undef,
file_name => 'file',
id => 'CA2005',
comment_name => 'cname',
id_type => 'bill',
commentary => 'some comment',
 
U

usenet

Darren said:
perhaps param('id' . $1) is multivalued?

If the OP would like to investigate Darren's question, s/he may use the
Dump function of CGI.pm, which is a good debugging tool. Simply put:

print Dump();

somewhere and it will show you all the param()eters, with their value
and structure (the dump is HTML formatted for display in a browser).

You will probably need to include Dump in your "use" statement, ie:

use CGI qw/:standard Dump/;

(note that "Dump" begins with an upper-case D).
 
A

Anno Siegel

Darren Dunham said:
perhaps param('id' . $1) is multivalued?

That's certainly what it looks like. If so,

%hash = (
# ...
id => scalar $qry->param('file_name' . $1),
# ...
);

should solve it.

Anno
 
X

xhoster

monkeys paw said:
For some reason i'm forced to use a scalar for
ONE hash element and not the rest. Here is how the
code behaves:

This works:

$qry = new CGI;

foreach (@names) {
if ($_ =~ /^newsummary(.*)$/ && $qry->param($_)) {
# I have no clue why 'id' must be a scalar to work. [sf]
my $id = $qry->param('id' . $1);

CGI's param can return a list if called in a list context. The hash
assignment is a list context. If you call it directly in the hash
assignment, and it happens to return a list of size other than 1, than your
hash gets mis-aligned.

If you want to avoid the temp variable you could put "scalar()" around the
call to param.

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top