Link to home
Start Free TrialLog in
Avatar of mmcw
mmcw

asked on

Check for double!

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"
Avatar of olthoff
olthoff

$test = "test|234|wert|234|test";
foreach $Item (split(/\|/, $test)) {
  $Items{$Item} = 0;
};
$test = join('|', keys(%Items));
print($test);
$test = "test|234|wert|234|test";
for (@fields = split '|', $test) {
    $hash{$_}++;
}
@fields = grep {exists $hash{$_} ? do {delete $hash{$_}; 1;} : undef;} @fields;
$test = join '|', @fields;
olthoff's answer is fine if you don't need to preserve the order of the fields in the line.
Avatar of ozo
`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;
Avatar of mmcw

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial