T
timdavis919
Hashes of Records -- who's smarter than me?
I'm hoping that someone who is smarter than me can solve this problem.
It shouldn't be hard -- I suspect I'm doing something obvious and
stupid, but I just can't see it.
I'm trying to create a hash of records, but the contents of all the
records in the hash seem to get overwritten by the contents of the
last record.
I wrote the simplified program below to demonstrate the problem.
In this simplified program I want to create three records (called
widgets) and assign them various characteristics (colors, powers,
etc.) by looping through arrays of those characteristics.
In the example below, I create a widget with name "thingy" color
"red" and power "hop". Then I put it into a hash of records
keyed by name and make sure I can read it out OK. Everything
seems to work.
Leaving out the looping for now, I just assign a new name and some
new characteristics to the record variable and write this second
record to the hash. The values from the second record (a blue
doodad that can skip) seem to overwrite the values in the first
record in the hash. So, although I put in a red thingy that can hop
and a blue doodad that can skip, I end up with two blue doodads --
one of which is still associatd with the hash key for the red thingy.
If anyone can see what the heck I'm doing wrong here, I would be the
happiest guy on the planet -- at least until I realize how embarrassing
my doubtlessly dumb mistake is...
-----------------------------------------------------------------------
CODE BEGINS HERE
-----------------------------------------------------------------------
#!/usr/local/bin/perl
use strict;
my @array_of_strings;
my @AoA_strings;
$AoA_strings[0] = [ @array_of_strings ];
my $widget = {
name => "",
colors => [@array_of_strings],
powers => [@AoA_strings],
};
my %hash_of_widgets;
my @allnames = ("thingy", "doodad", "whatzit");
my @allcolors = ("red", "blue", "green");
my @allpowers = (
["hop", "skip", "jump"],
["nip", "bite", "decapitate"],
);
# Create a thingy that is red and can hop
$widget->{name} = $allnames[0];
$widget->{colors}[0] = $allcolors[0];
$widget->{powers}[0][0] = $allpowers[0][0];
print "First, we make a $widget->{name} that is $widget->{colors}[0]
and can $widget->{powers}[0][0] \n\n";
#Put the red hopping thingy in the hash
$hash_of_widgets {$widget->{name}} = $widget;
print "Second, when we put it in the hash, we have there:\n";
for my $key (sort keys %hash_of_widgets)
{
print " for the hash key $key: a $hash_of_widgets{$key}->{name} that
is $hash_of_widgets{$key}->{colors}[0] and can
$hash_of_widgets{$key}->{powers}[0][0] \n";
}
print "\n";
# Create a doodad that is blue and can skip
$widget->{name} = $allnames[1];
$widget->{colors}[0] = $allcolors[1];
$widget->{powers}[0][0] = $allpowers[0][1];
print "Third, we make a $widget->{name} that is $widget->{colors}[0]
and can $widget->{powers}[0][0] \n\n";
$hash_of_widgets {$widget->{name}} = $widget;
print "Fourth, when we put him in the hash we have:\n";
for my $key (sort keys %hash_of_widgets)
{
print " for the hash key $key: a $hash_of_widgets{$key}->{name} that
is $hash_of_widgets{$key}->{colors}[0] and can
$hash_of_widgets{$key}->{powers}[0][0] \n";
}
I'm hoping that someone who is smarter than me can solve this problem.
It shouldn't be hard -- I suspect I'm doing something obvious and
stupid, but I just can't see it.
I'm trying to create a hash of records, but the contents of all the
records in the hash seem to get overwritten by the contents of the
last record.
I wrote the simplified program below to demonstrate the problem.
In this simplified program I want to create three records (called
widgets) and assign them various characteristics (colors, powers,
etc.) by looping through arrays of those characteristics.
In the example below, I create a widget with name "thingy" color
"red" and power "hop". Then I put it into a hash of records
keyed by name and make sure I can read it out OK. Everything
seems to work.
Leaving out the looping for now, I just assign a new name and some
new characteristics to the record variable and write this second
record to the hash. The values from the second record (a blue
doodad that can skip) seem to overwrite the values in the first
record in the hash. So, although I put in a red thingy that can hop
and a blue doodad that can skip, I end up with two blue doodads --
one of which is still associatd with the hash key for the red thingy.
If anyone can see what the heck I'm doing wrong here, I would be the
happiest guy on the planet -- at least until I realize how embarrassing
my doubtlessly dumb mistake is...
-----------------------------------------------------------------------
CODE BEGINS HERE
-----------------------------------------------------------------------
#!/usr/local/bin/perl
use strict;
my @array_of_strings;
my @AoA_strings;
$AoA_strings[0] = [ @array_of_strings ];
my $widget = {
name => "",
colors => [@array_of_strings],
powers => [@AoA_strings],
};
my %hash_of_widgets;
my @allnames = ("thingy", "doodad", "whatzit");
my @allcolors = ("red", "blue", "green");
my @allpowers = (
["hop", "skip", "jump"],
["nip", "bite", "decapitate"],
);
# Create a thingy that is red and can hop
$widget->{name} = $allnames[0];
$widget->{colors}[0] = $allcolors[0];
$widget->{powers}[0][0] = $allpowers[0][0];
print "First, we make a $widget->{name} that is $widget->{colors}[0]
and can $widget->{powers}[0][0] \n\n";
#Put the red hopping thingy in the hash
$hash_of_widgets {$widget->{name}} = $widget;
print "Second, when we put it in the hash, we have there:\n";
for my $key (sort keys %hash_of_widgets)
{
print " for the hash key $key: a $hash_of_widgets{$key}->{name} that
is $hash_of_widgets{$key}->{colors}[0] and can
$hash_of_widgets{$key}->{powers}[0][0] \n";
}
print "\n";
# Create a doodad that is blue and can skip
$widget->{name} = $allnames[1];
$widget->{colors}[0] = $allcolors[1];
$widget->{powers}[0][0] = $allpowers[0][1];
print "Third, we make a $widget->{name} that is $widget->{colors}[0]
and can $widget->{powers}[0][0] \n\n";
$hash_of_widgets {$widget->{name}} = $widget;
print "Fourth, when we put him in the hash we have:\n";
for my $key (sort keys %hash_of_widgets)
{
print " for the hash key $key: a $hash_of_widgets{$key}->{name} that
is $hash_of_widgets{$key}->{colors}[0] and can
$hash_of_widgets{$key}->{powers}[0][0] \n";
}