Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts

    not deleting hash values

    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.

    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

    Code:
    '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

    Code:
    '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
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #2
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,869
    Thanks
    2
    Thanked 163 Times in 158 Posts
    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.

    Code:
    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;
    Last edited by FishMonger; 09-13-2009 at 06:10 PM.

  • #3
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    Thanks FishMonger, that has helped with part of it.

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

    Code:
    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
    Last edited by bazz; 09-13-2009 at 11:56 PM.
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #4
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,869
    Thanks
    2
    Thanked 163 Times in 158 Posts
    That could be reduced to:
    Code:
        delete $tariff_to_terms{$tariff_id} if ! grep /$tariff_id/, @tariffs;

  • #5
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    Thanks FishMonger, that works a treat. I'll have to read up on grep.

    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •