Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Last Element of Hash

by LazerRed (Pilgrim)
on Sep 25, 2003 at 20:41 UTC ( [id://294245]=note: print w/replies, xml ) Need Help??


in reply to Last Element of Hash

I was thinking something like:

my @elements = ( keys %hash ); foreach (@elements){ if ( $_ eq $elements[-1] ){ #Do something to last element }else{ #Do something to an item in element } }

To me, it's easier to see it that way... I'm still rather "young" on Perl :-)
caveat: above will give warnings if @elements is full of numbers...

LR

Update: Changed @elements[-1] to $elements[-1]

Whip me, Beat me, Make me use Y-ModemG.

Replies are listed 'Best First'.
Re: Re: Last Element of Hash
by bart (Canon) on Sep 26, 2003 at 13:26 UTC
    caveat: above will give warnings if @elements is full of numbers...
    No it won't. You can always use a number as a string without any warning. The reverse isn't always true.

    And besides, here, the elements are originally hash keys. Hash keys are always strings.

      Thanks for the clarification... I still get confused sometimes over the little details, but I'm slowly getting the hang of it :-)

      LR

      Whip me, Beat me, Make me use Y-ModemG.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-26 02:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found