in reply to How do I remove duplicate numeric elements of an array and preserve alphabetic elements?

Try this:

#! perl -slw use strict; use Data::Dump qw[ pp ]; my( @ordered, %grouped ); while( <DATA> ) { chomp; my @pair = split ',', $_; $ordered[ @ordered ] = $pair[ 0 ] unless exists $grouped{ $pair[ 0 + ] }; push @{ $grouped{ $pair[0] } }, $pair[1]; } #pp \@ordered, \%grouped; print "$_|", join ',', @{ $grouped{ $_ } } for @ordered; __DATA__ 20055111,YOUSLAV, 20055111,YURT, 20055111,TENWIMPL, 20011271,YOUSLAV, 20011271,WUMARTHE 20011541,YOUSLAV, 20011541,TENWIMPL, 20102741,WEDLOFOU, 20102741,YOUSLAV, 20102741,YURT, 20102741,KUPLYSO, 20102741,TENWIMPL, 20155505,YOUSLAV, 20155505,YURT, 20155505,TENWIMPL, 20147155,YOUSLAV, 20147155,KUPLYSO, 20147155,FRIMA,
Output:
C:\test>1215831.pl 20055111|YOUSLAV,YURT,TENWIMPL 20011271|YOUSLAV,WUMARTHE 20011541|YOUSLAV,TENWIMPL 20102741|WEDLOFOU,YOUSLAV,YURT,KUPLYSO,TENWIMPL 20155505|YOUSLAV,YURT,TENWIMPL 20147155|YOUSLAV,KUPLYSO,FRIMA

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit
  • Comment on Re: How do I remove duplicate numeric elements of an array and preserve alphabetic elements?
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: How do I remove duplicate numeric elements of an array and preserve alphabetic elements?
by Marshall (Canon) on Jun 04, 2018 at 20:29 UTC
    I like your code++.

    A couple of minor nits:
    I personally try to avoid using subscripts in favor of assigning a name to variables in a split.
    In this case, I'm not sure what the number represents (or the name), I'm sure the OP knows better a better description than us.
    I would use a push instead of array assignment to @ordered, just because it seems more natural to me.
    This is minor stuff - no problem at all with your code.

    Update: I guess I don't know what is supposed to happen if say YOUSLAV appeared twice for 20055111 or whether that is even possible to occur. If that is possible, the OP should clarify.

    #! perl -slw use strict; use Data::Dump qw[ pp ]; my( @ordered, %grouped ); while( <DATA> ) { chomp; my ($number, $name) = split ',', $_; push (@ordered, $number ) unless exists $grouped{$number }; push @{ $grouped{ $number } }, $name; } #pp \@ordered, \%grouped; print "$_|", join ',', @{ $grouped{ $_ } } for @ordered; =prints 20055111|YOUSLAV,YURT,TENWIMPL 20011271|YOUSLAV,WUMARTHE 20011541|YOUSLAV,TENWIMPL 20102741|WEDLOFOU,YOUSLAV,YURT,KUPLYSO,TENWIMPL 20155505|YOUSLAV,YURT,TENWIMPL 20147155|YOUSLAV,KUPLYSO,FRIMA =cut __DATA__ 20055111,YOUSLAV, 20055111,YURT, 20055111,TENWIMPL, 20011271,YOUSLAV, 20011271,WUMARTHE 20011541,YOUSLAV, 20011541,TENWIMPL, 20102741,WEDLOFOU, 20102741,YOUSLAV, 20102741,YURT, 20102741,KUPLYSO, 20102741,TENWIMPL, 20155505,YOUSLAV, 20155505,YURT, 20155505,TENWIMPL, 20147155,YOUSLAV, 20147155,KUPLYSO, 20147155,FRIMA,
      You said "I guess I don't know what is supposed to happen if say YOUSLAV appeared twice for 20055111 or whether that is even possible to occur" It's not possible for the term to appear twice with the number. His solution appears to work very well!
        My question stands.
        Nothing in the spec precludes this.
        Nobody including me provides a coded solution for that situation.
        This was a "hey, what if x? question".

        I don't know what you mean by "His solution appears to work very well!"
        If you mean code from BrowserUk, my code is the same, with some minor style adjustments.
        I did that just to show a few nice Perl features.

Re^2: How do I remove duplicate numeric elements of an array and preserve alphabetic elements?
by jzelkowsz (Novice) on Jun 07, 2018 at 12:59 UTC
    I'm very pleased to say your solution is working. I remmed out the chomp statement and put in two file handling statements and now it's doing exactly what I need. I had to install the "Data::Dump" module. Thank you, this is great work and very slick!