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"
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"
$test = "test|234|wert|234|test";
for (@fields = split '|', $test) {
$hash{$_}++;
}
@fields = grep {exists $hash{$_} ? do {delete $hash{$_}; 1;} : undef;} @fields;
$test = join '|', @fields;
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.
`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;
$test = "test|234|wert|234|test";
%hash=();
$test = join '|',grep{!$hash{$_}++} split/\|/,$test;
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
foreach $Item (split(/\|/, $test)) {
$Items{$Item} = 0;
};
$test = join('|', keys(%Items));
print($test);