Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2009/05/08

Self-Introduced Bugs

So, I have 3 different SQL statements that handle three different cases, and the mechanism that dealt with it spat out hashes. (I code Perl. Just making this clear early on.)

I wanted to unify the three hashes into one hash. The way I liked best was to push pointers to the hashes into an array, iterating through that array, and taking each hash and dumping the values into a master hash.


my %te_ids ;
my @loop ;
push @loop, \%te_ids1 ;
push @loop, \%te_ids2 ;
push @loop, \%te_ids3 ;
for my $hash ( @loop ) {
for my $k ( keys %te_ids2 ) {
$te_ids{ $k } = 1 ;
}
}


That might not be the best way to join hashes, but it is the way I liked. I have the join code once, so if I decide "hey, this works much better!", I don't have to fix it more than one place.

But do you see the problem? Do you see why I wasn't getting values off %te_ids1 and %te_ids3? Do you see it?

I didn't. Not for a while.

(And to be honest, I used sort keys %te_ids2, when sorting the keys added to the overhead with no benefit at all. Just a piece of cargo-cult programming I have to rid my mind of.)

This is the fixed version. The version where I actually use the refs to hashes I shove into the array.


my %te_ids ;
my @loop ;
push @loop, \%te_ids1 ;
push @loop, \%te_ids2 ;
push @loop, \%te_ids3 ;
for my $hash ( @loop ) {
for my $k ( keys %$hash ) {
$te_ids{ $k } = 1 ;
}
}

1 comment: