Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

remove duplicates from an array!

by nashkab (Novice)
on Jan 07, 2008 at 22:06 UTC ( [id://660955]=perlquestion: print w/replies, xml ) Need Help??

nashkab has asked for the wisdom of the Perl Monks concerning the following question:

I Have an Array of Arrays which looks like this:-
@ICE = 30F-WKS(tab)`1781183799.xxxx1' (tab) IC---; 30F-WKS (tab) `1781183799.xxx11' (tab) IC---; ADM34A3F9 (tab) `1781183799.41455' (tab) IC---;
IF TWO RECORDS EXIST FOR THE SAME INSTANCE.I WANT TO KEEP THE FINAL RECORD AND REMOVE THE PREVIOUS RECORDS. FOR EXAMPLE, COMPUTER 30F-WKS HAS TWO RECORDS.I WANT TO REMOVE THE FIRST RECORD AND KEEP THE SECOND.

The result will be:-

ICE= 30F-WKS (tab) `1781183799.xxx11' (tab) IC---; ADM34A3F9 (tab) `1781183799.41455' (tab) IC---;

I have used the following code: my @ICEU; my %seen; my @ICEU = grep { !$seen{$_->[0]}++ } @ICE; print Dumper \%seen; The output I am getting is:- $VAR1 = { '' => 1218 };

Your advise will be appreciated

Replies are listed 'Best First'.
Re: remove duplicates from an array!
by shmem (Chancellor) on Jan 07, 2008 at 22:56 UTC
    Not only is this incomprehensible, but the ink is ugly and the paper is from the wrong kind of tree.
    -- Professor, EECS, George Washington University
    Your array declaration is incorrect and unorderly. Whitespace scattered wildly, tabs not written as such... say rather
    @ICE = ( "30F-WKS\t`1781183799.xxxx1'\tIC---;", "30F-WKS\t`1781183799.xxx11'\tIC---;", "ADM34A3F9\t`1781183799.41455'\tIC---;", );
    The result you see in %seen indicates that your array @ICE contains 1218 elements, of which none is an array. The elements, if I get correctly what you mean by
    30F-WKS (tab) `1781183799.xxx11' (tab) IC---;

    are strings with tab separated fields. If you want to make each string into an array and check for uniqueness of their first element, you have to split them and make anonymous arrays of the first element from each split return, along with the original string as the second element, grep the list of anonymous arrays for definedness in %seen (that part of your code is correct) and finally extract the second element (which is the original string) from each anonymous array grep returns:

    my @ICEU = map { $_->[1] } grep { !$seen{$_->[0]}++ } map { [ (split / +\t/, $_)[0], $_ ] } @ICE;

    Always use -w!

    Your second declaration of @ICEU with my masks the first one.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: remove duplicates from an array!
by FunkyMonk (Chancellor) on Jan 07, 2008 at 22:38 UTC
    When I transform your code into something I can run (it really does help us and you):
    my @ICE = map { [ split /\s*\(tab\)\s*/ ] } <DATA>; my %seen; my @ICEU = grep { !$seen{$_->[0]}++ } @ICE; print Dumper \@ICE; print Dumper \%seen; __DATA__ 30F-WKS(tab)`1781183799.xxxx1' (tab) IC---; 30F-WKS (tab) `1781183799.xxx11' (tab) IC---; ADM34A3F9 (tab) `1781183799.41455' (tab) IC---;

    I get

    $VAR1 = [ [ '30F-WKS', '`1781183799.xxxx1\'', 'IC---; ' ], [ '30F-WKS', '`1781183799.xxx11\'', 'IC---; ' ], [ 'ADM34A3F9', '`1781183799.41455\'', 'IC---; ' ] ]; $VAR1 = { 'ADM34A3F9' => 1, '30F-WKS' => 2 };

    Which looks pretty much as it should, doesn't it?

    It looks like Hercynium is right. @ICE doesn't contain what you think it does

Re: remove duplicates from an array!
by assemble (Friar) on Jan 07, 2008 at 22:42 UTC
Re: remove duplicates from an array!
by kirillm (Friar) on Jan 07, 2008 at 22:47 UTC
      Hi Kirill, Thanks for the response. In My previous post the data was in a file. Now for this problem the data is in an array.
        In My previous post the data was in a file. Now for this problem the data is in an array.

        That doesn't matter. The answer to "How do I remove duplicates from a set of data?" is the same. You may have to transform your data into a form more amenable to this operation, but the techniques for removing duplicates are the same.

Re: remove duplicates from an array!
by apl (Monsignor) on Jan 08, 2008 at 02:16 UTC
    Your advise will be appreciated

    Drop the class.
Re: remove duplicates from an array!
by Hercynium (Hermit) on Jan 07, 2008 at 22:23 UTC
    Could you please post a dump of what you have in @ICEU? The rest of your code looks correct, but perhaps the data you're searching through isn't what you expect?
    print Dumper \@ICEU;
      Hi it only displays the first line:-
      30F-WKS(tab)`1781183799.xxxx1' (tab) IC---;

      can you suggest anternate coding

        Looks like the people in the posts below have the answer for you... your data isn't what you think it is, but it can be transformed pretty easily. :) Cheers!
Re: remove duplicates from an array!
by Anonymous Monk on Jan 08, 2008 at 21:02 UTC

    Don't YELL!

    All-uppercase is harder to read than mixed-case, and may be interpreted as being written by somebody who doesn't believe in proof-reading.

    Advice? This is a very common problem; search through the monastery (check out the link on this page called "Super Search"). Many of the monks (including me) can be grouchy, and we react badly to questions which do not seem to have been well-thought out. And watch out for the English-usage nazis.

      Seconded: Please do not yell in the monastery. Some of us are meditating.
Re: remove duplicates from an array!
by Anonymous Monk on Jan 09, 2008 at 14:39 UTC
    I would love it if you stopped getting your way with every homework question that you shamelessly ask here. I've been studying perl for three months, have no experience in programming whatsoever, and even I find your questions trivial. I suggest you try to answer them for yourself first; bothering experts with your school exercises seems nothing less than a waste of human resources.
Re: remove duplicates from an array!
by Anonymous Monk on Jan 09, 2008 at 00:00 UTC
    Good god that is horrid!! Can't anyone in Bangalore write decent Perl???

    I am sorry I think you are stuck at the help desk forever

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://660955]
Front-paged by tye
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-23 22:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found