On 05/02/00, ""Scott K Purcell" <[EMAIL PROTECTED]>" wrote:
> I have a hash whose keys are organized by the values.
> %hash = (
> 'apple' => '1',
> 'peach' => '2',
> 'pear' => '3',
> 'plum' => '4',
> );
> 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.
This problem has gotten me to learn about and play a bit
with tied hashes, so I thought I'd just share the
latest version of my solution:
#!/home/dougw/perl/bin/perl -w
package IdxHash;
use strict;
use Tie::Hash;
# perl 5.6 function
our @ISA = qw(Tie::StdHash);
# use this if not 5.6:
# use vars qw(@ISA);
# @ISA = qw(Tie::StdHash);
sub TIEHASH {
my $class = shift;
bless { @_ }, $class;
}
sub DELETE {
my $hshref = shift;
my $key = shift;
my $value = $hshref->{$key};
delete $hshref->{$key};
for (values %$hshref) {
$_-- if $_ > $value;
}
}
package main;
use strict;
tie my %hsh, 'IdxHash', qw(e 5 a 1 b 2 c 3 d 4);
print "Before\n";
while (my ($key, $value) = each %hsh) {
print "[$key]=[$value]\n";
}
delete $hsh{b};
print "After\n";
while (my ($key, $value) = each %hsh) {
print "[$key]=[$value]\n";
}
---
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]