in reply to Re: Re: duplicate lines in array
in thread duplicate lines in array

If you want to golf you may increase yout handicap by shortening
for (@rec) { /(H\(\d+\))/ && do { push @{$count{$1}}, $_ } } for (keys %count) { if ($#{$count{$_}}) { print "$_\n" for @{$count{$_}} } }
to just
/(H\(\d+\))/ && push(@{$count{$1}},$_."\n") for (@rec); print map @{$count{$_}},grep $#{$count{$_}},keys %count;
Anyone volunteering to explain my code?

Replies are listed 'Best First'.
Re: Re: Re: Re: duplicate lines in array
by Roger (Parson) on Jan 23, 2004 at 14:43 UTC
    Another go... ;-)

    /(H\(\d+\))/,push@{$count{$1}},"$_\n"for@rec; print map{$#{$count{$_}}?@{$count{$_}}:''}keys%count;

Re: Re: Re: Re: duplicate lines in array
by Fletch (Bishop) on Jan 23, 2004 at 15:01 UTC

    Bah, you call that golfing? :) You used multi-letter variable names and more than one space.

    /(H\(\d+\))/&&push@{$c{$1}},"$_\n"for@r; print map@{$c{$_}},grep$#{$c{$_}},keys%c;
      How comes you missed this?
      /H\(\d+\)/&&push@{$c{$&}},$_.$/for@r;
      You could even save "keys" because no value will be a key:
      print map@{$c{$_}},grep$#{$c{$_}}>0,%c;