Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: array/hash - reference and dereference

by fullermd (Priest)
on Jan 09, 2009 at 03:04 UTC ( [id://735061]=note: print w/replies, xml ) Need Help??


in reply to array/hash - reference and dereference

I'm confused on just what you're trying to do. I'm not sure you aren't as well.

# How to create a hash with keys only?

That's nonsensical. Keys are a way to find a value. You can create a hash of keys whose value is undef or 0 or an empty string or any temporary placeholder you want, but you can't have a "hash with keys only".

@insert = \%hash;

This creates a 1-array whose first element is a reference to an empty hash. I don't think that's what you want to do.

The rest of the code is at least superficially reasonable (if you put %ages before the lines where you try and reference it), but what's the purpose of %hash? What are you wanting to end up with?

Replies are listed 'Best First'.
Re^2: array/hash - reference and dereference
by Anonymous Monk on Jan 09, 2009 at 03:33 UTC
    O..my mistake..%hash %ages Sorry.
    %ages = ('Martin' => 0, 'Sharon' => 0, 'Rikke' => 0); #this way? @insert = \%hash; #at this point, what is the correct way to save the hash into an array +? $insert[0] = $ages->{‘Sharon’}; $insert[1]=$ages->{'Martin’}; %ages = ('Martin' => 28, 'Sharon' => 35, 'Rikke' => 29); : : $sqlquery -> execute(@insert);
    My idea is: When the script start, I don't have the hash values. So,I want to create "pointer" points to the hash. And "save" the specific hash value into an array. Later, when the hash values are fetched, I can just run:
    $sqlquery -> execute(@insert);
    Please advise. Thanks.

      It sounds like a lot more trouble than just making a mkinsert() function.

      %ages = [...] # Finally filled in $sqlquery->execute(mkinsert(%ages));

      You could store references to the hash elements into the array instead. But you'd have to explicitly dereference them, and it would mostly serve to confuse people reading the code. Why not make it simple?

      O..Sorry...my mistake..%hash %ages
      %ages = ('Martin' => 0, 'Sharon' => 0, 'Rikke' => 0); #this way? @insert = \%ages; #at this point, what is the correct way to save the hash into an array +? $insert[0] = $ages->{‘Sharon’}; $insert[1]=$ages->{'Martin’}; %ages = ('Martin' => 28, 'Sharon' => 35, 'Rikke' => 29); : : $sqlquery -> execute(@insert);
      My idea is: When the script start, I don't have the hash values. So,I want to create "pointer" points to the hash. And "save" the specific hash value into an array. Later, when the hash values are fetched, I can just run:
      $sqlquery -> execute(@insert);
      Please advise. Thanks.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-25 14:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found