Solved

remove dupes from array

Posted on 2014-03-11
5
185 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
Comment Utility
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
Comment Utility
Thanks Wilcox! I'll give it a try. Yes, ordering does matter.
Nacht
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
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
Comment Utility
Worked perfectly. Thanks!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 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

6 Experts available now in Live!

Get 1:1 Help Now