[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Check for double!

Posted on 1999-12-30
6
Medium Priority
?
193 Views
Last Modified: 2010-03-05
Hello,

How to check and delete double entries?

$test = "test|234|wert|234|test"

after the check the result has to be:

$test = "test|234|wert"
0
Comment
Question by:mmcw
6 Comments
 
LVL 1

Expert Comment

by:olthoff
ID: 2315436
$test = "test|234|wert|234|test";
foreach $Item (split(/\|/, $test)) {
  $Items{$Item} = 0;
};
$test = join('|', keys(%Items));
print($test);
0
 
LVL 5

Expert Comment

by:prakashk021799
ID: 2315515
$test = "test|234|wert|234|test";
for (@fields = split '|', $test) {
    $hash{$_}++;
}
@fields = grep {exists $hash{$_} ? do {delete $hash{$_}; 1;} : undef;} @fields;
$test = join '|', @fields;
0
 
LVL 5

Expert Comment

by:prakashk021799
ID: 2315522
olthoff's answer is fine if you don't need to preserve the order of the fields in the line.
0
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.

 
LVL 85

Expert Comment

by:ozo
ID: 2315840
`perldoc -q "How can I extract just the unique elements of an array"`
$test = "test|234|wert|234|test";
%hash=();
$test = join '|',grep{!$hash{$_}++} split/\|/,$test;
0
 

Author Comment

by:mmcw
ID: 2316367
Question to ozo:

Is it possible to make it so that lowwercae or uppercase doesn't matter and that test and Test is the same!

$test = test|Test|wat

becomes

$test = test|wat
0
 
LVL 85

Accepted Solution

by:
ozo earned 200 total points
ID: 2316377
$test = join '|',grep{!$hash{lc $_}++} split/\|/,$test;
#or, if you don't need to preserve the order or case with olthoff's answer
@Items{split/\|/,lc $test} = ();
$test = join('|', keys(%Items));
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

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

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 …
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.…
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

591 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