Solved

remove dupes from array

Posted on 2014-03-11
5
186 Views
Last Modified: 2014-03-12
Hi,
I'm trying to work out a problem with removing dupe records from a database.
One method I'm trying is by using straight SQL.
The other is to dump the data into a perl array and remove the dupes that way.

So, for example data lets say I have this

@names = "john;lennon;nyc;100","paul;mccartney:la;101,"john;lennon;la:103;"ringo;starr;la;104";

So, I have an array with 4 values. Each value has four parts separated by a semicolon. the fields in the array are (fname;lname;office;id). I only need the values that don't contain a dupe value, so in this case I don't need john lennon because he exists in two offices, I only need the other three beatles. How can I remove both instances of john lennnon and keep their other two beatles in an array structure?

Thanks,
Nacht
0
Comment
Question by:nachtmsk
  • 3
5 Comments
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 39921280
Does the ordering of the array matter?  If not, there may be a simpler way.  This will preserve order.
my @names = ("john;lennon;nyc;100","paul;mccartney:la;101,"john;lennon;la:103;"ringo;starr;la;104");
my %cnt;
foreach my $n (@names) {
    my ($fn, $ln) = split /;/, $n;
    $cnt{"$fn;$ln;"}++;
}
foreach my $c (keys %cnt) {
    delete $cnt{$c} if $cnt{$c}>1;
}
my $rx = join '|', keys %cnt;
@names = grep m{^(?:$rx)}, @names;

Open in new window

0
 

Author Comment

by:nachtmsk
ID: 39921306
Thanks Wilcox! I'll give it a try. Yes, ordering does matter.
Nacht
0
 
LVL 84

Expert Comment

by:ozo
ID: 39921466
Did you see
perldoc -q "How can I remove duplicate elements from a list or array?";
@names = ("john;lennon;nyc;100","paul;mccartney:la;101","john;lennon;la:103","ringo;starr;la;104");
$hash{$_}++ for map{/([^;:]*[;:][^:;]*)/,$_}@names;
@names=grep{$hash{(/([^;:]*[;:][^:;]*)/)[0]}}@names;
0
 

Author Comment

by:nachtmsk
ID: 39921639
Thanks ozo. I didn't see that and after having seen it, I'm  not sure what's going on.
Will that remove dupes based on the first and second fields in each array element? If not, how do I change it to do that. The map function has always confused me.

Thanks,
N
0
 

Author Closing Comment

by:nachtmsk
ID: 39923880
Worked perfectly. Thanks!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now