Scott K Purcell wrote:
> 
> Hello,
> I have a hash whose keys are organized by the values.
> eg.
> %hash = (
>       'apple' => '1',
>       'peach' => '2',
>       'pear' => '3',
>       'plum' => '4',
> );
> 
> And all is good.
> But now, if I delete an item, lets say 'peach', I need some way to move the
> values up and keep the order, just minus the one.
> eg.
> apple = 1;
> pear = 2;
> plum = 3;

Assuming that all keys and all values are unique, you could keep
another hash that's the inverse of the original

%inverse_hash = reverse %hash

Then create a subroutine to handle the delete. I would explicitly pass
the hashes and key, but that's just me.


sub delete_key
{
   my ( $key ) = @_;

   my $value = $hash{$key};

   delete $hash{$key};

   foreach ( sort keys %invers_hash )
   {
      next until $_ > $value;
      $hash{$_}--;
   }

   %inverse_hash = (); # clear it to be anal ;-)

   %inverse_hash = reverse %hash;

};


joe

---
You are currently subscribed to perl-win32-users as: [archive@jab.org]
To unsubscribe, forward this message to
         [EMAIL PROTECTED]
For non-automated Mailing List support, send email to  
         [EMAIL PROTECTED]

Reply via email to