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

Re^2: removing duplicates from an array of hashes

by AnomalousMonk (Archbishop)
on Apr 17, 2014 at 17:32 UTC ( [id://1082666]=note: print w/replies, xml ) Need Help??


in reply to Re: removing duplicates from an array of hashes
in thread removing duplicates from an array of hashes

Just a matter of idle curiosity... I notice you use  @$ref-1 instead of  $#$ref for obtaining the max array index. Why do you prefer this? If  $[ were ever changed (but don't do that!),  @$ref-1 would yield an incorrect value for max index. (Although I suppose that one could argue that to be $[-safe, one ought always to iterate over  $[ .. $#array rather than  0 .. $#array to safely visit the entire range of  @array indices.)

Also, in your second code example
    splice @$ref,-$removed;
might be written as the (presumably) marginally faster
    $#$ref -= $removed;

Update: I should make it clear that this post was prompted mainly by the non-idiomatic nature of the  @$ref-1 expression. Any "lack-of-safety" issue is only a kind of cayenne pepper icing on the cake.

Replies are listed 'Best First'.
Re^3: removing duplicates from an array of hashes
by bigj (Monk) on Apr 18, 2014 at 14:23 UTC
    Reasoning is simple:

    I didn't program for a long time (close to 10yrs), I still were sure that $#array is the last item, but I was too lazy to look up, how to note it when it is a $arrayref, so I just took 0..@$arrayref-1 what works same here and for at least where I stopped, it was same idiomatic. Every programmer in the world understand 0..length(array)-1 in a heartbeat, only Perl cracks will understand $[..$#array.

    I wasn't aware that I could just $#array -= $n to make the array smaller. To be honest, I'm not sure whether I'd like it. It works only in so rare cases where we basical want to  pop @array, $n and don't care about what were the last items. splice is a clear idiom that we intend to remove entries out of an array and then specify which. If the special case were more often, o.k., but how often do we even use splice? IMHO that is even rare, most of the time we pop, shift or slice with @array[4..7,11..13] and so on, so no need to trick us selfs just to trick us. Code should be easy.

    Anyway, pretty to cool to have learned some new tricks in Perl :-)

    Greetings,
    Janek Schleicher

Log In?
Username:
Password:

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

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

    No recent polls found