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

Dear Monks, I am new to perl, have spent so much time for this pgm with unfruitful results please help me with it. I have an array which consists of an unique Id followed by sequence of characters. for Example 1f:6 aaaabbbbcccdddgi 1f:8 ggggggiiiihhhhbb 2c:7 ffffffttttthhhhh 2f:9 ggksvskbskbgskbs 1f:0 bfgfggvkvgkgbkui now I need to eliminate the duplicates for the ID where first 2 letters of the ID is same, and keep the only the first sequence of array (for ID which have duplicates ) in result. so desired output is going to be: 1f:6 aaaabbbbcccdddgi 2c:7 ffffffttttthhhhh 2f:9 ggksvskbskbgskbs Regards

Replies are listed 'Best First'.
Re: eliminating duplicates with filters
by toolic (Bishop) on Feb 07, 2011 at 22:06 UTC
    have spent so much time for this pgm with unfruitful results please help me with it
    It would be best to post what you have tried so far. Post your code and input data inside "code" tags to make your post more readable (Writeup Formatting Tips).

    Perl has great documentation available on your command line:

    perldoc -q duplicate

    You should try to use a hash. See also perldsc.

Re: eliminating duplicates with filters
by ikegami (Patriarch) on Feb 07, 2011 at 22:30 UTC

    [ Use <p> to mark the start of paragraphs, and wrap computer text and other preformatted text using <c>...</c>. ]

    while (<>) { my ($key) = /^(..)/s; print if !$seen{$key}++; }
      Hi, thanks for the reply.. Actually I am reading that array data from a text file so how can I apply it there?? I think I have to first identify the ID elements in the text through pattern matching, then eliminate all duplicate ID's and their corresponding sequence. but here 1f:a & if:b are duplicate IDs too i.e data after colon (:) isn't considered so they both are considered as same id and duplicates has to be eliminated. Hope I am clear now !! please help me with it.. Thanks in advance
        If you want to loop over an array instead of a text file, replace
        while (<>) {
        with
        for (@array) {
Re: eliminating duplicates with filters
by Anonymous Monk on Feb 07, 2011 at 22:05 UTC