Getting Value of nested hash attributes -- HASH confusion
I have to following template that lists table data with a checkbox to the left and a text area to the right -- special thanks to Andrew for helping me figure out the HTML. The data comes from: my $q = "SELECT c.ID AS ID, u.screenname AS SNAME, a.name AS ANAME, a.descrip AS DESCRIP, c.season AS SSN, c.year AS YR, ch.name AS CHNAME FROM challenge c, accomp_type a, usuario u, jugador j, charity ch WHERE c.ACC_TYPE_ID=a.ID AND ch.ID=c.CHAR_ID AND c.PLR_ID=j.ID AND j.USER_ID=u.ID"; my $Chlngs = $dbh->selectall_hashref( $q, 'ID' ); <div class="container" style="margin-top:20px;"> <div class="row"> <div class="col-xs-12"> <form role="form" action="openchallenges" method="POST"> <h3 class="text-center">Open To Do</h3> <div class="well" style="max-height: 300px;overflow: auto;"> <ul class="list-group fancy-list-items"> <!-- <ul class="list-group checked-list-box"> --> <table style="width:100%"> <% FOREACH ID IN Chlngs.keys.sort %> <tr class="list-group-item"> <td width="5"> <input type="checkbox" name="<% Chlngs.$ ID.NAME %>"</input> </td> <td width="70"><% Chlngs.$ID.SNAME %></td> <td width="75"><% Chlngs.$ID.ANAME %></td> <td width="35"><% Chlngs.$ID.SSN %></td> <td width="35"><% Chlngs.$ID.YR %></td> <td width="250"><% Chlngs.$ID.CHNAME %></td> <td width="550"><% Chlngs.$ID.DESCRIP %></td> <td><input type="text" name="<% Chlngs.$ID.ID %>" size="8" </td> <% END %> </tr> </ul> </table> </div> <hr class="colorgraph"> <div class="row"> <div class="col-xs-12 col-md-12"><input type="submit" value="Submit" class="btn btn-primary btn-block btn-lg"></div> </form> </div> </div> </div> However, after getting the template to work I am once again stumped in how to capture the value of the dynamically named hash ref -- specifically the second one from the text box. I have tried various permutations below -- some of which were very graciously provided to me in an earlier similar thread -- but all seem to only give me either a hash reference, the value of ID or this error when I place the number 7 into the text field: Can't use string ("7") as a HASH ref while "strict refs" in use at /home/richard/Dancer2/MyApp/bin/../lib/MyApp.pm line 463. my $input_hash; foreach my $key ( keys %$Chlngs ) { print "Checking Chlgs: " . $key . " for value.\n"; if ( param( $key ) ) { # paramter has value #$input_hash->{ $key } = $Chlngs->{ $key }; $input_hash->{ $key } = param( $key ); #print $key, $input_hash->{ $key }, "\n"; #print %{ $input_hash->{1} }; print $input_hash->{1}{ID}, "\n"; # $sth->execute($PLR_ID, $key, $input_hash->{Season}, $input_hash->{Year}, $input_hash->{CharID}); } } Thanks for any help. Richard
On Mon, 2015-08-31 at 16:37 -0500, Richard Reina wrote: [...]
<td width="550"><% Chlngs.$ID.DESCRIP %></td> <td><input type="text" name="<% Chlngs.$ID.ID %>"
So, the name of the input parameter is an ID number.
However, after getting the template to work I am once again stumped in how to capture the value of the dynamically named hash ref
In which case it will be returned as a scalar using param(): my $value = param( $key ); Which will return, for example, "7", if that is what is in the textbox.
foreach my $key ( keys %$Chlngs ) { print "Checking Chlgs: " . $key . " for value.\n"; if ( param( $key ) ) { # paramter has value #$input_hash->{ $key } = $Chlngs->{ $key }; $input_hash->{ $key } = param( $key );
^^ So just capture the value here, but just in a scalar Is there a reason for using a hash?
#print $key, $input_hash->{ $key }, "\n"; #print %{ $input_hash->{1} }; print $input_hash->{1}{ID}, "\n"; # $sth->execute($PLR_ID, $key, $input_hash->{Season}, $input_hash->{Year}, $input_hash->{CharID});
^^ And update it here But I'm not quite sure why you are also updating the other values? Where are they coming from? If they haven't changed, then don't bother passing them into the SQL query, just do an execute on the captured value.
2015-08-31 17:07 GMT-05:00 Andrew Beverley <andy@andybev.com>:
On Mon, 2015-08-31 at 16:37 -0500, Richard Reina wrote: [...]
<td width="550"><% Chlngs.$ID.DESCRIP %></td> <td><input type="text" name="<% Chlngs.$ID.ID %>"
So, the name of the input parameter is an ID number.
However, after getting the template to work I am once again stumped in how to capture the value of the dynamically named hash ref
In which case it will be returned as a scalar using param():
my $value = param( $key );
Which will return, for example, "7", if that is what is in the textbox.
foreach my $key ( keys %$Chlngs ) { print "Checking Chlgs: " . $key . " for value.\n"; if ( param( $key ) ) { # paramter has value #$input_hash->{ $key } = $Chlngs->{ $key }; $input_hash->{ $key } = param( $key );
^^ So just capture the value here, but just in a scalar Is there a reason for using a hash?
No, no good reason to be using a hash. Been hammering so much that I forgot that everything looks like a nail. Thank you for pointing out that I don't need a hash here. What a relief. my $value = param( $key ) is exactly what I needed. Thank you once again. I will someday need to fly to London and buy you several pints! Richard
participants (2)
-
Andrew Beverley -
Richard Reina