...

View Full Version : not deleting hash values



bazz
09-13-2009, 09:47 AM
Hi,

making progress now on hashes. YAY!

what I have is shown below. I am trying to delete hash key/values, where they are no longer required but they do not delete. What I also notice is that the new hash, is created but the correspoding key/values pairs in the original hash are changed as if there's some sort of referencing going on.

here is my code.



while ( ( $tariff_id, $hash) = each %tariff_to_terms )
{
foreach my $tariff (@tariffs)
{
if ( $tariff eq $tariff_id ) #83
{
$new_hash{$tariff_id}=$hash;
}
else
{
delete($tariff_to_terms{$tariff_id}); # get rid of the other hash data but it isn't
}
}
}


here is the session dump of the original hash



'tariff_to_terms' => {
'85' => {
'Adult' => {
'65.00' => 'prpn',
'32.50' => 'pppns',
'45.00' => 'sopn'
}
},
'83' => {
'Up to 12 yrs' => {
'25.44' => 'pacpn'
},
'Adult' => {
'30.00' => 'pppns',
'60.00' => 'prpn',
'40.00' => 'sopn'
}
},
'86' => {
'Adult' => {
'150.00' => 'ppk'
}
}
},



and here is the original hash after the new one is created



'tariff_to_terms' => {
'85' => {
'Adult' => {
'65.00' => 'prpn',
'45.00' => 'sopn',
'32.50' => 'pppns'
}
},
'83' => ${$VAR1}->{'_DATA'}->{'new_hash'}->{'83'},
'86' => {
'Adult' => {
'150.00' => 'ppk'
}
}
},


Q's.

1. were I to need it someday, is the 83 key available still in this hash (above), or should I just get the data from the new hash? I mean why should it be in both places? maybe that line is just telling me where the data was moved to??

2. why is my code section not removing 85 and 86 from the hash? I know I can just delete the hash completely but I want to understand these issues.

any explanation or pointers to good tutorials, greatly appreciated.

bazz

FishMonger
09-13-2009, 04:33 PM
Is %tariff_to_terms the root hash, or is it a key within a more complex HoH?

Instead of using while/each to loop over the hash, use foreach to loop over the keys.

It would be more efficient to use a %tariffs hash instead of multiple loops over the @tariffs array, but as long as the array is small, it won't be too bad.

Instead of using a foreach loop on the @tariffs array, it would be cleaner to use grep.


foreach my $tariff_id ( keys %tariff_to_terms ) {

if ( grep /$tariff_id/, @tariffs ) {
$new_hash{$tariff_id} = delete $tariff_to_terms{$tariff_id};
}
}

print Dumper \%tariff_to_terms;
print Dumper \%new_hash;

bazz
09-13-2009, 11:25 PM
Thanks FishMonger, that has helped with part of it.

I think I might have simplified it :eek: lol
Now it only retains the necessary keys and related data



foreach my $tariff_id ( keys %tariff_to_terms ) {

if ( grep /$tariff_id/, @tariffs ) {
#print qq( wanted tid=$tariff_id );
} else {
#print qq( unwanted tid=$tariff_id : );
delete $tariff_to_terms{$tariff_id};
}
}



bazz

FishMonger
09-14-2009, 01:04 AM
That could be reduced to:

delete $tariff_to_terms{$tariff_id} if ! grep /$tariff_id/, @tariffs;

bazz
09-14-2009, 08:15 AM
Thanks FishMonger, that works a treat. I'll have to read up on grep.

bazz



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum