?
Solved

remove dupes from array

Posted on 2014-03-11
5
Medium Priority
?
191 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 2000 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
 
LVL 1

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
 
LVL 1

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
 
LVL 1

Author Closing Comment

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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
Suggested Courses

764 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