Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: How do I remove duplicate numeric elements of an array and preserve alphabetic elements? -- oneliner

by Discipulus (Canon)
on Jun 04, 2018 at 15:52 UTC ( [id://1215852]=note: print w/replies, xml ) Need Help??


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

Hello jzelkowsz and welcome to the monastery and to the wonderful world of Perl!

As you already got useful answers I propose you a short version(pay attention to windows double quotes!):

perl -F"," -lane "push @{$h{$F[0]}},$F[1]}{print map{$_.'|'.(join',',@ +{$h{$_}}).qq(\n)}keys %h" data.txt 20155505|YOUSLAV,YURT,TENWIMPL 20102741|WEDLOFOU,YOUSLAV,YURT,KUPLYSO,TENWIMPL 20011541|YOUSLAV,TENWIMPL 20011271|YOUSLAV,WUMARTHE 20147155|YOUSLAV,KUPLYSO,FRIMA 20055111|YOUSLAV,YURT,TENWIMPL

See perlrun to get all these perl switches explained, but use -MO=Deparse to see the oneliner exploded and more readable (the curly braces in $F[1]}{print are a trick called esquimo greeting;):

perl -MO=Deparse -F"," -lane "push @{$h{$F[0]}},$F[1]}{print map{$_.'| +'.(join',',@{$h{$_}}).qq(\n)}keys %h" BEGIN { $/ = "\n"; $\ = "\n"; } LINE: while (defined($_ = <ARGV>)) { chomp $_; our(@F) = split(/,/, $_, 0); push @{$h{$F[0]};}, $F[1]; } { print map({$_ . '|' . join(',', @{$h{$_};}) . "\n";} keys %h); } -e syntax OK

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
  • Comment on Re: How do I remove duplicate numeric elements of an array and preserve alphabetic elements? -- oneliner
  • 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? -- oneliner
by jzelkowsz (Novice) on Jun 07, 2018 at 13:21 UTC
    perl -F"," -lane "push @{$h{$F[0]}},$F[1]}{print map{$_.'|'.(join',',@{$h{$_}}).qq(\n)}keys %h" data.txt

    This definitely works too.

    June 8th 2018 Discipulus added code tags

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1215852]
help
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found