Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: How can I keep the first occurrence from duplicated strings? -- oneliner

by Discipulus (Canon)
on Aug 30, 2023 at 07:58 UTC ( [id://11154141] : note . print w/replies, xml ) Need Help??


in reply to How can I keep the first occurrence from duplicated strings?

Hello,

many variations but at glance it seems to me that this is missing: use a hash and put vualues into an array (choroba suggested this..).

Being an opportunity to produce a oneliner, I cannot resist to :)

cat uniqfirst.txt nick 5 nick 10 nick 20 john 78 erik 9 erik 12 perl -lanE "push@{$r{$F[0]}},$F[1]}{say join' ',$_,$r{$_}[0]for keys%r +" uniqfirst.txt erik 9 nick 5 john 78 perl -MO=Deparse -lanE "push@{$r{$F[0]}},$F[1]}{say join' ',$_,$r{$_}[ +0]for keys%r" BEGIN { $/ = "\n"; $\ = "\n"; } use feature 'current_sub', 'evalbytes', 'fc', 'postderef_qq', 'say', ' +state', 'switch', 'unicode_strings', 'unicode_eval'; LINE: while (defined($_ = readline ARGV)) { chomp $_; our @F = split(' ', $_, 0); push @{$r{$F[0]};}, $F[1]; } { say join(' ', $_, $r{$_}[0]) foreach (keys %r); } -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 can I keep the first occurrence from duplicated strings? -- oneliner
  • Download Code