http://www.perlmonks.org?node_id=751213

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!

Replies are listed 'Best First'.
Re: Check if object at start of array
by ikegami (Patriarch) 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.