Solved

remove dupes from array

Posted on 2014-03-11
5
188 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to strip .csv from file name 9 81
Add additional column to .csv using Perl. 8 140
quoting a comma separated list 20 84
Regex rule to match two different url 5 71
I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 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