Re: adding to a hash
by MrYoya (Monk) on May 13, 2003 at 19:10 UTC
|
The .= operator only applies to strings.
To add to a hash, try this:
$variables{variable_name} = get_page_content("names");
Please please get yourself a copy of Learning Perl, it'll pay off :) | [reply] [d/l] [select] |
Re: adding to a hash
by Limbic~Region (Chancellor) on May 13, 2003 at 19:31 UTC
|
$hash{'key1'} = 4;
$hash{'key2'} = 5;
You will notice that the % sigil has change to a $. Remember
that $ looks like the s in scalar. It means just a single
piece of data. When you get a little more advanced, you can check
out hash slices.
There are plenty of resources for learning Perl:
http://learn.perl.org/ which even has free on-line books
Perl FAQ for frequently asked questions
Perlmonks Q&A
Learning Perl - (dead tree) O'Reilly
Cheers and keep trying
L~R
| [reply] [d/l] |
Re: adding to a hash
by pzbagel (Chaplain) on May 13, 2003 at 19:29 UTC
|
Generally speaking, if you are getting variable name/value pairs then adding them to the hash is simply a matter of:
$variables{$varname}=$varvalue;
Because you mention that you don't want to delete variables that are already there, I wonder if you mean that some of the variables you are reading in may already be in the hash. If that is the case, you can make sure they are not in the hash by doing something like this when you assign the values:
$variables{$varname}||=$varvalue;
That little ||= keeps a value in the hash if it is there or assigns a new key/value pair if it is not defined yet.
The question here is: What exactly is get_page_content() returning? A scalar? array? reference? If you can tell us this, we can give you a more specific anwer.
Cheers | [reply] [d/l] [select] |
|
$h{key} = 0;
$h{key} ||= "foo";
print $h{key};
If you really want to check for definedness, you should use defined(). Often, though, there are situations where undef is a valid value too and in those cases you should use exists() to check whether the hash key exists yet. Here is one way to do that succinctly:
$h{$key} = "value" unless exists $h{$key};
-sauoq
"My two cents aren't worth a dime.";
| [reply] [d/l] [select] |
Re: adding to a hash
by Thelonius (Priest) on May 13, 2003 at 19:51 UTC
|
Not unless get_page_content("names") returns a key-value paired list. Well, duh, but that's implied by his question.Plus, that's just a silly way of doing it. I don't think it's silly, just inefficient. How about this:
sub pushhash (\%@) {
my $hash = shift;
my $key;
$$hash{$key} = shift while defined($key = shift);
}
my %a;
$a{cow} = "COW1";
$a{dog} = "DOG1";
pushhash %a, cow => "COW2", bull => "Terrier";
for (sort keys %a) {
print "$_ => $a{$_}\n";
}
| [reply] [d/l] |
Re: adding to a hash
by Thelonius (Priest) on May 13, 2003 at 19:38 UTC
|
%variables = (%variables, get_page_content("names"));
although it's not the most efficient thing in the world. | [reply] [d/l] |
|
Not unless get_page_content("names") returns a key-value paired list. Plus, that's just a silly way of doing it.
------ We are the carpenters and bricklayers of the Information Age. Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement. Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.
| [reply] |
Re: adding to a hash
by powerhouse (Friar) on May 13, 2003 at 20:18 UTC
|
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) | [reply] [d/l] |
|
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).
| [reply] [d/l] [select] |
Re: adding to a hash
by powerhouse (Friar) on May 13, 2003 at 20:28 UTC
|
How effecient would this be:
When I call the get_page_content("names"); what if I passed the current hash there, and just had the sub get_page_content intercept it, and instead of doing my $temp_vars;
I could do: my ($type,%temp_vars) = @_;
Then it would be pre-populated with all the current data, and add all the new data, then pass it back in the return.
Would that be a "secure" or good way of doing it?
thx,
Richard
| [reply] [d/l] [select] |
|
That would work, but it would be more efficient to pass the hash by reference:
get_page_content(\%variables, "names");
# instead of %variables = get_page_content("names");
sub get_page_content {
my ($hashref, $type) = @_;
my $dbh = DB_File::connect();
$sth = $dbh->prepare (qq{ SELECT `name`,`value` FROM `page_variabl
+es` WHERE `type` = ? });
$sth->execute($type);
while(my ($db_name,$db_content) = $sth->fetchrow_array()) {
$$hashref{$db_name} = $db_content;
}
$sth->finish();
}
| [reply] [d/l] |
Re: adding to a hash
by BrowserUk (Patriarch) on May 14, 2003 at 12:40 UTC
|
Tom Christiansen, the author of the well-worth reading FMTEYEWtKaPiP shows this neat function.
sub hpush(\%@) {
my $href = shift; # NB: not %
while ( my ($k, $v) = splice(@_, 0, 2) ) {
$href->{$k} = $v;
}
}
Which you could use to this
hpush(%variables, get_page_content("names"));
Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
| [reply] [d/l] [select] |
Re: adding to a hash
by Skeeve (Parson) on May 14, 2003 at 06:58 UTC
|
Flame me if it's already told before :-) I couldn't find it.
How about returning a reference to your %temp_vars and adding your new variables like this:
# This would be the return statement of get_page_content
return \%temp_vars;
# And this would be the call:
$new_variables = get_page_content("names");
# $new variables is a reference to your result
@variables{keys %$new_variables}=values %$new_variables;
Of course this will overwrite all variables that exist in both %tempvars and %variables with the value found in %temp_vars.
| [reply] [d/l] |