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

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,
  • Comment on Re^2: How do I remove duplicate numeric elements of an array and preserve alphabetic elements?
  • Download Code

Replies are listed 'Best First'.
Re^3: How do I remove duplicate numeric elements of an array and preserve alphabetic elements?
by jzelkowsz (Novice) on Jun 07, 2018 at 13:11 UTC
    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.