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

Re: adding to a hash

by powerhouse (Friar)
on May 13, 2003 at 20:18 UTC ( [id://257886]=note: print w/replies, xml ) Need Help??


in reply to adding to a hash

Wow, thanks for all the replies :o)

I am using the sub get_page_content to get variables in a database.

I took the advice of another monk and added a type to the table, so I am passing the "type" only, and it is getting all those variables and putting them in the hash. So It looks like this:

sub get_page_content { my $type = shift; my $dbh = DB_File::connect(); $sth = $dbh->prepare (qq{ SELECT `name`,`value` FROM `page_variabl +es` WHERE `type` = ? }); $sth->execute($type); my %temp_vars; # WILL THIS KILL THE REST OF THE VALUES? while(my ($db_name,$db_content) = $sth->fetchrow_array()) { $temp_vars{$db_name} = $db_content; } $sth->finish(); return(%temp_vars); }
Since I call this with %variables = get_page_content("name"); that would replace the whole hash, right? OR would it just append the new variables to this list? They will always be unique, as the column `name` has to be unique, or it will error out, when adding new records to the db.

thx,
Richard


BTW, I do have quite a few Perl books, but I have only read two of them("Perl Cookbook" and "Perl and Mysql for the Web"). I'm by no means a Perl guru. ;o)
When I have more time on my hands I'll devour the Others! :o)

Replies are listed 'Best First'.
Re: Re: adding to a hash
by Fastolfe (Vicar) on May 13, 2003 at 23:49 UTC
    Remember that = is an assignment operator, not an addition or concatenation operator. When you use it, you are telling Perl to set the left-hand side equal to the right-hand side. All entries in the hash will be replaced.

    You have two options: You can either iterate through the replacement key/value pairs and set the $hash{$key} = $value, or if your list of keys and values are known ahead of time, a hash slice might let you make a change to a group of hash entries with one statement (e.g. @hash{@new_keys} = @new_values).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://257886]
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: (4)
As of 2024-04-24 07:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found