Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: removing duplicates entries from an array

by ikegami (Patriarch)
on Jan 06, 2006 at 17:09 UTC ( #521549=note: print w/replies, xml ) Need Help??


in reply to removing duplicates entries from an array

What's with all the options on that regexp? igcgxm?? And assuming $serviceName and $dll are strings, not regexp, you don't even quote your variables?

Your sorting before removing the duplicates, when it would be cheaper to sort after they have been removed (since you algorithm don't take into account the fact that the array is sorted).

join(" ", split " ", $elem) is very weird too.

When you want unique, use a hash!

sub RDup { my %unique; $unique{lc($_)} = $_ foreach @_; return sort values %unique; } my @unique; foreach $d (@dllExeLines) { if ($d =~ /\Q$serviceName/) { foreach $f (split(/,/, $d)) { if ($f =~ /\Q$dll/i) { push @unique, $dll; } } } } @unique = RDup(@unique);

Replies are listed 'Best First'.
Re^2: removing duplicates entries from an array
by kulls (Hermit) on Jan 06, 2006 at 17:18 UTC
    We can directly replace   push @unique, $dll; with  $unique{$dll}=1 .
    why not ?
    - kulls
      Yes, RDup could be eliminated. I didn't remove it for the sake of reusability.
      my %unique; foreach $d (@dllExeLines) { if ($d =~ /\Q$serviceName/) { foreach $f (split(/,/, $d)) { if ($f =~ /\Q$dll/i) { $unique{lc($dll)} = $dll; } } } } my @unique = sort values %unique;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2023-09-27 09:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?