Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

bulding a hash

by mnlight (Scribe)
on Jul 19, 2007 at 16:37 UTC ( [id://627567]=perlquestion: print w/replies, xml ) Need Help??

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

Can someone explain why this is not working.
I capture a single column but more then one row.
my $rows = $sth->fetchall_arrayref();
I then return the refs from the subroutine.
$options = get_options() <
I then pass it to another subroutine
change_dboption ( \$dbh, $DB, $date, $options );
Then I loop through the rows and try to build the hash with a count
my $count = 1; my %option_ref = (); foreach $option ($options){ %option_ref = {$count => @$option}; $count++; }
I feel like I am reinventing the wheel by creating the hash but I want the user to be able to input an integer to represent the options returned from the database. So the value they supply will be used to call up the value from the hash.

Replies are listed 'Best First'.
Re: bulding a hash
by ikegami (Patriarch) on Jul 19, 2007 at 16:57 UTC

    You didn't provide get_options, so I have to guess that $options is a reference to an array. If so,

    foreach $option ($options){

    should be

    foreach $option (@$options){

    because you want to loop over every item in the array referenced by the scalar $options.

    my @array = qw( a b ); my $scalar = 'xyz'; my $reference = \@array; for (@array ) { print("$_\n"); } print("\n"); for ($scalar ) { print("$_\n"); } print("\n"); for ($reference ) { print("$_\n"); } print("\n"); for (@$reference) { print("$_\n"); } print("\n");
    a b xyz ARRAY(0x226b04) # A ref is just a scalar a b
Re: bulding a hash
by philcrow (Priest) on Jul 19, 2007 at 16:59 UTC
    Perhaps you might rather have DBI build the hash? Then you could try fetchall_hashref.

    Phil

    The Gantry Web Framework Book is now available.
      I thought of using fetchall_hashref but I am not sure how to use the values inputed by the user as the key. The table I am querying has no values like that. Also I only reference one column and I need a way to point to each individual row value. e.g. My option types are displayed as
      1 read 2 write 3 copy
      I want the user to enter the number value but in the script I need to use the char string.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (8)
As of 2024-04-19 09:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found