Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Last Element of Hash

by LazerRed (Pilgrim)
on Sep 25, 2003 at 20:41 UTC ( #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.


Comment on Re: Last Element of Hash
Select or Download Code
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
Node Status?
node history
Node Type: note [id://294245]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2014-12-26 10:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (171 votes), past polls