Re: Removal of duplicated element in array.
by moritz (Cardinal) on May 06, 2013 at 14:01 UTC
|
How exactly does it not work? What exactly is in $1? What output do you expect? Are you aware that the code as-is splits on newline characters, which means that "duplicate items" actually mean "duplicate lines"?
If you want to deduplicate a list, I recommend List::MoreUtils::uniq.
| [reply] [d/l] |
|
|
$1 is essentially a list of email addresses.
dddd@fgg.com
cccc@fgg.com
eeee@fgg.com
dddd@fgg.com
| [reply] |
|
|
my %seen = map {$_=>1} <DATA>;
use Data::Dumper;
print Dumper \%seen;
__DATA__
dddd@fgg.com
cccc@fgg.com
eeee@fgg.com
dddd@fgg.com
*UPDATE: If you care about the order of display then you can use this:
print join "\n", sort{$a cmp $b} keys %seen;
*Note: That was based on the assumption, that the original ORDER of data was inconsequential. Limbic~Region Thanks for pointing that out.
If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me
| [reply] [d/l] [select] |
|
|
|
|
Re: Removal of duplicated element in array.
by 2teez (Vicar) on May 06, 2013 at 14:07 UTC
|
| [reply] |
|
|
Thanks for sharing , the link has very useful stuff.
| [reply] |
Re: Removal of duplicated element in array.
by hdb (Monsignor) on May 06, 2013 at 14:20 UTC
|
One issue are the parantheses in split("(\n)",$match). They preserve the delimiters \n as part of your array and are also present in the final result @r. Try
split /\n/, $match
instead. You can also do it as a one-liner:
my @r = keys { map { $_ => 1 } split /\n/, $match };
UPDATE: I mean the parantheses in "(\n)", not the outer ones. | [reply] [d/l] [select] |
|
|
I tried my @match_to_array = split (/\n/,$match); , the \n are not preserved.
my @r = keys { map { $_ => 1 } split /\n/, $match }; has " Type of arg 1to keys must be hash (not annonymous hash ) " error.
Guess I did something wrong for the -rep ? Sorry If I did so.
| [reply] [d/l] [select] |
|
|
my %seen = map { $_ => 1 } split /\n/, $match;
my @r = keys %seen;
Hope this now works for you. And I still assume that you want to get rid of the \n? Apologies for any confusion I caused. | [reply] [d/l] |
|
|
|
|
/\n/ doesn't preserve the delimiters =X .
| [reply] [d/l] |
Re: Removal of duplicated element in array.
by Rahul6990 (Beadle) on May 06, 2013 at 14:05 UTC
|
Hi,
For me the same code is working
my @match_to_array = (a,b,c,a,d,c,b,r);
my %seen = ();
my @r = ();
foreach my $a (@match_to_array) {
unless ($seen{$a}) {
push @r, $a;
$seen{$a}++;
}
}
print "@r";
print "\n@match_to_array";
Can you explain how it is not working , with actual and expected output.
| [reply] [d/l] |
|
|
Hi Rahul6990 ,
this line :my @match_to_array = (a,b,c,a,d,c,b,r); #Oops
should either be
my @match_to_array = ('a','b','c','a','d','c','b','r'); OR
my @match_to_array = qw(a b c a d c b r);
If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me
| [reply] [d/l] [select] |
|
|
dddd@fgg.com
cccc@fgg.com
eeee@fgg.com
dddd@fgg.com
The above is both the input and output. The output is still the same after running the code. | [reply] |
Re: Removal of duplicated element in array.
by Laurent_R (Canon) on May 06, 2013 at 18:44 UTC
|
As I already told you on another forum, the problem is most probably in the split. Try this instead:
my @match_to_array = split /\n/, $match;
| [reply] [d/l] |
|
|
I tried what you mentioned, it still didn't work.
I also tried totally eliminating the split operator altogether, and directly assign the $match to the @match_to_array , it didn't work too.
| [reply] |
Re: Removal of duplicated element in array.
by 2teez (Vicar) on May 07, 2013 at 04:25 UTC
|
Hi tty1x,
A few snapshot observations and 2 'kobo' suggestion:
-
A number of solution previously given to you actually work, only that some may not preserve the order of data.
- This does work for me, which is not far from your OP, except that I have to create the variable $match.
use warnings;
use strict;
my $match = <<'EOF';
dddd@fgg.com
cccc@fgg.com
eeee@fgg.com
dddd@fgg.com
EOF
my %seen;
my @r;
foreach my $v ( split /\n/, $match ) {
unless ( $seen{$v}++ ) {
push @r, $v;
}
}
$" = "\n";
print "@r";
- Please, as much as possible try not to use the variable $a, since function sort uses it
Tested this out on codepad an online compiler/interpreter.
If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me
| [reply] [d/l] |
|
|
Perl 5.8.0? What a pitty :-(
| [reply] |
|
|
| [reply] |
Re: Removal of duplicated element in array.
by Laurent_R (Canon) on May 07, 2013 at 09:28 UTC
|
Hi,
I do not know what you changed, but taking your original program, changing the split pattern to /\n/ (and adding a few prints just to show the content of the various variables) show that this works perfectly.
my $match = "foo1\nfoo2\nfoo3\nfoo4\nfoo5\nfoo3\nfoo2";
print '$match = ', "\n", $match, "\n\n";
my @match_to_array = split /\n/, $match;
print '@match_to_array = ', "@match_to_array \n\n";
my %seen = ();
my @r = ();
foreach my $a (@match_to_array) {
unless ($seen{$a}) {
push @r, $a;
$seen{$a}++;
}
}
print '@r = ', "@r";
This is now the output:
$ perl remove_duplicates.pl
$match =
foo1
foo2
foo3
foo4
foo5
foo3
foo2
@match_to_array = foo1 foo2 foo3 foo4 foo5 foo3 foo2
@r = foo1 foo2 foo3 foo4 foo5
The duplicate values (foo3 and foo2) have been duly removed.
| [reply] [d/l] [select] |
|
|
The problem is simply that the OP is making the suggested corrections on a piece of code that is different from what he has presented (as he has admitted in his cross-post on a different forum).
@ tty1x: we cannot guess what other bugs you have in your code if you don't tell us what this code is.
| [reply] |