in reply to the last added element of a hash
Hello Lennotoecom,
I would re-write this idea as follows:
#! perl use strict; use warnings; use List::Util 'max'; my %h = ( '11c' => 'C', b => 'B', e22 => 'E', aaaaa => 'AAAA', ); if (%h) { my $last = max map { \$_ } values %h; printf "The last element added to the hash was %s\n", $$last; } else { print "No elements have been added to the hash\n"; }
This gives the same result, but:
This script’s output (like the title of this node) is still somewhat misleading, however, as “last element added” really means “last element added of those still remaining in the hash.” For example, if you add elements X, Y, and Z, and then delete element Z, this script will output Y as the “last element added,” which isn’t strictly correct.
But the real problem with this whole strategy is that it relies on an implementation detail of the perl interpreter; namely, that it always assigns new hash values to memory addresses above those of previously-assigned values. Is this guaranteed to be the case? Will it always be true of perl implementations in the future? I don’t know, and for that reason I would advise against this approach until and unless it can be proven to be valid now and also future-proof.
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: the last added element of a hash
by LanX (Saint) on Jul 31, 2014 at 09:38 UTC | |
by Discipulus (Canon) on Jul 31, 2014 at 10:47 UTC | |
|