#!/usr/bin/perl
package OHash;

sub TIEHASH
{
    my $self=shift;
    my $hRef={};

    $hRef->{'hash'}={};
    $hRef->{'keys'}=[];
    $hRef->{'values'}=[];
    $hRef->{'index'}=0;

    return bless $hRef, $self;
}
sub STORE
{
    my ($self, $key, $value)=@_;
    my $index=$self->{'hash'}->{$key};
    if (defined $index)
    {
        $self->{'keys'}->[$index]=$key;
        $self->{'values'}->[$index]=$value;
    }
    else
    {
        $self->{'hash'}->{$key}=scalar @{$self->{'values'}};
        push @{$self->{'keys'}}, $key;
        push @{$self->{'values'}}, $value;
    }
}
sub FETCH
{
    my ($self, $key)=@_;
    my $index=$self->{'hash'}->{$key};
    if (defined $index)
    {
        return $self->{'values'}->[$index];
    }
    else
    {
        return undef;
    }
}
sub FIRSTKEY
{
    my $self=shift;
    $self->{'index'}=0;
    return $self->{'keys'}->[$self->{'index'}];
}
sub NEXTKEY
{
    my $self=shift;
    $self->{'index'}++;
    return $self->{'keys'}->[$self->{'index'}];
}
sub EXISTS
{
    my ($self, $key)=@_;
    exists $self->{'hash'}->{$key};
}
sub DELETE
{
    my ($self, $key)=@_;
    my $index=$self->{'hash'}->{$key};
    if (defined $index)
    {
        foreach my $key (keys(%{$self->{'hash'}}))
        {
            $self->{'hash'}->{$key}-- if ($self->{'hash'}->{$key} > $index);
        }
        splice @{$self->{'keys'}}, $index, 1;
        splice @{$self->{'values'}}, $index, 1;
        delete $self->{'hash'}->{$key};
    }
}
sub CLEAR    { %{$_[0]} = () }

1;