Scott K Purcell wrote, on Tuesday, May 02, 2000 08:53
: 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;
So the values in the hash are all unique numbers from 1 to n?
Sounds like you want to invert the hash to an array, splice
that element out, and re-invert the array to a hash. I suppose
the only reason it's a hash in the first place is to make it
searchable?
my @fruits;
while (my ($fruit, $idx) = each %hash) {
$fruits[$idx] = $fruit;
}
my $del_idx = $hash{$badfruit};
delete $hash{$badfruit};
splice @fruits, $del_idx, 1;
for (my $i = $del_idx; $i < @fruits; $i++) {
$hash{$fruits[$i]} = $i;
}
Or, you could delete the hash element, and then go through the
hash and subtract 1 from every hash value greater than the
value deleted. I have no idea which would be faster.
Joe
==============================================================
Joseph P. Discenza, Sr. Programmer/Analyst
mailto:[EMAIL PROTECTED]
Carleton Inc. http://www.carletoninc.com
219.243.6040 ext. 300 fax: 219.243.6060
Providing Financial Solutions and Compliance for over 30 Years
---
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]