Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Removal of duplicated element in array.

by tty1x (Novice)
on May 06, 2013 at 13:56 UTC ( #1032293=perlquestion: print w/ replies, xml ) Need Help??
tty1x has asked for the wisdom of the Perl Monks concerning the following question:

The following is the code for removing duplicated data, but it doesn't work.

Any guidance is greatly appreciated, at my wits end now =/.

#$1 is the matched regex, including duplicated elements. my $match = $1."\n"; my @match_to_array = split("(\n)",$match); my %seen = (); my @r = (); foreach my $a (@match_to_array) { unless ($seen{$a}) { push @r, $a; $seen{$a}++; } } print @r;

Comment on Removal of duplicated element in array.
Download Code
Re: Removal of duplicated element in array.
by moritz (Cardinal) on May 06, 2013 at 14:01 UTC
      $1 is essentially a list of email addresses.

      dddd@fgg.com
      cccc@fgg.com
      eeee@fgg.com
      dddd@fgg.com
      

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

      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
Re: Removal of duplicated element in array.
by 2teez (Priest) on May 06, 2013 at 14:07 UTC
      Thanks for sharing , the link has very useful stuff.
Re: Removal of duplicated element in array.
by hdb (Parson) 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.

       /\n/ doesn't preserve the delimiters =X .
      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.

        Might be that you are using an older version of Perl? I'm on 5.16. Here is a two line version:

        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.

Re: Removal of duplicated element in array.
by Laurent_R (Parson) 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;
      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.
Re: Removal of duplicated element in array.
by 2teez (Priest) 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
      Perl 5.8.0? What a pitty :-(
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        Yeap! I know. When the OP kept complaining about virtually all the solution given and he/she said he/she is using v5.10.1.
        I thought if the solution am presenting works on a version as old as v5.8.0, then it should work on v5.10.1. That was why I mentioned that.

        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
Re: Removal of duplicated element in array.
by Laurent_R (Parson) 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.

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1032293]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (16)
As of 2014-08-28 16:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (264 votes), past polls