Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Check if object at start of array

by Anonymous Monk
on Mar 17, 2009 at 14:50 UTC ( #751213=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have an array which I add an object to. I then sort the array. How do I check if the object I have just added is at the start of the array? Thanks!

Comment on Check if object at start of array
Re: Check if object at start of array
by ikegami (Pope) on Mar 17, 2009 at 14:52 UTC
    use Scalar::Util qw( refaddr ); defined(refaddr($sorted[0])) && refaddr($o) == refaddr($sorted[0])

    Scalar::Util

    Update: Added defined check.

Re: Check if object at start of array
by RMGir (Prior) on Mar 17, 2009 at 14:53 UTC
    I suggest reading perlref and searching for "cheap numeric compare of references"...

    Mike

      That will work in most circumstances, but note these exceptions:

      The linked code can produce undesirable results (false positives, false negatives) if the inserted object or an object in the array overrides numification or numerical equivalency.

      The linked code can produce undesirable results (false positives, warnings) if the array contains non-references.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://751213]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (8)
As of 2014-09-23 05:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (210 votes), past polls