use warnings; use strict; # should be used almost always my %new_strings; # create the hash containing new values while (<>) { # read STDIN and/or files specified on the command line chomp; # cut the newline symbol my ($first,$id) = (split ",",$_,3); # read first two columns and discard all others my $numeric = /(,[\d,]+)$/; # use regexp to match numeric values at the end of the line unless (exists $new_strings{$id}) { # if this data was found the first time, initialise the value in the hash $new_strings{$id}="$first,$id"; } $new_strings{$id}.=$numeric; # append the numeric data found } foreach (sort keys %new_strings) { # for each datum found in the second column, in alphabet order print "$new_strings{$_}\n"; # print the full string with all numeric data found }