Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Remove redundency from an array

by Eimi Metamorphoumai (Deacon)
on Sep 25, 2007 at 17:00 UTC ( #640992=note: print w/replies, xml ) Need Help??


in reply to Re: Remove redundency from an array
in thread Remove redundency from an array

The idea is sound. Depending on how many duplicates there are, it may end up more expensive (since you're sorting the entire array, instead of only the unique items). Also, the line @array = (sort shift @sorted) is really...odd. Why not just @array = shift @sorted? Additionally, I think instead of while (my $element = shift @sorted) { I would have just used for (@sorted){, unless there's a reason you really want to destroy the sorted version as you go.

Replies are listed 'Best First'.
Re^3: Remove redundency from an array
by lyklev (Pilgrim) on Sep 25, 2007 at 21:28 UTC
    You are right, it should have been just

    @array = (shift @sorted)

    (the parentheses are to make it an array assignment).

    If there are many duplicates it might be more efficient to first remove the duplicates before sorting. But there is no transforming the array into a hash and back.

    I can't tell which is more efficient. My intention was to show a different approach. It shows again that there are many ways to do similar things.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2020-10-24 17:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My favourite web site is:












    Results (246 votes). Check out past polls.

    Notices?