Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: the last added element of a hash

by Athanasius (Archbishop)
on Jul 31, 2014 at 09:24 UTC ( [id://1095711]=note: print w/replies, xml ) Need Help??


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:

  • compiles under use strict;
  • runs without warnings under use warnings; and
  • handles the case where the hash is empty.

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
    > always assigns new hash values to memory addresses above those of previously-assigned values. Is this guaranteed to be the case? 

    No, as soon as an address is freed again it might be reused.

    Just try to delete an entry and create new ones ...:)

    Cheers Rolf

    (addicted to the Perl Programming Language and ☆☆☆☆ :)

    update

    Not a meditation and poorly worded!

    moved to SOPW.

      In fact this smell like things you never have to attempt..
      good point LanX!

      But you Lennotoecom, what do you think about? your meditation is so poor in words...

      Can i add: when you play with memory's addresses in Perl you are always going in a wrong direction.

      L*
      There are no rules, there are no thumbs..
      Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1095711]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-20 04:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found