in reply to Removing redundancy

dr_jgbn,
When I see the words ultra efficient script - I think that this is either:
  • A case not to use Perl
  • Perl is fine and you are paranoid about premature optimization

    With that said and given your example data - I would:

    #!/usr/bin/perl -w use strict; my %seen; my @data; open (INPUT,"file") or die "Error accessing file : $!"; while (<INPUT>) { chomp; my ($col1, $col2) = split /\t/; if (exists $seen{$col1}) { $data[$seen{$col1} - 1] .= " $col2"; } else { $seen{$col1} = push @data , "$col1\t$col2"; } } print "$_\n" foreach(@data);

    Cheers - L~R

    Update: Completely re-wrote code to be unique as my original solution closely mirrored others

  • Use array instead of hash to store data
  • Use hash to determine array element
  • Use fact that push returns new number of elements to store in hash
  • Replies are listed 'Best First'.
    A reply falls below the community's threshold of quality. You may see it by logging in.