Solved

remove dupes from array

Posted on 2014-03-11
5
190 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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 …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Six Sigma Control Plans

717 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