in reply to Last Element of Hash

waswas-fng hit the nail on the head I believe, seeing hashes iterate in a seemingly random order, I can only assume that you actually SORT your keys into some kind of order, whence you can find the last key alphanumerically, am I right?

In which case, liz's answer is a good one, because depending on the order of the sort, you can always find not only the last key, but the last one alphanumerically, or the first one, if your sort is ascending instead.

Liz's answer is even the more useful if you're looking for the 'last' out of several hashes. In the real-life example Im giving below, I created two hashes labelled 'atoms' and 'protons'. Their keys are the actual line numbers from a flat file, where each atom/proton had it's own line.

Hence by iterating the line numbers themselves(extracted here from a 'residue'), I can get the true last atom/line of a residue.

foreach my $ref (sort keys %{$self->{'residues'}{'1'}}){ $last=$self->{'atoms'}{$ref} if defined $self->{'atoms'}{$ref}; $last=$self->{'protons'}{$ref} if defined $self->{'protons'}{$ref} +; }
Cheers
Sam