in reply to appending values based on criteria
#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; use 5.14.0; my %stuff; #extract the header row. #use the regex to remove the linefeed, because #we can't chomp it inline like this. #works since perl 5.14 #otherwise we could just chomp (@header) later. my ( $id, @header ) = split( /\|/, <DATA> ); chomp (@header); while (<DATA>) { #turn this row into a hash of key-values. my %row; ( $id, @row{@header} ) = split(/\|/); #diag print print Dumper \%row; #iterate each key, and insert into $row. foreach my $key ( keys %row ) { push( @{ $stuff{$id}{$key} }, $row{$key} ); } } #diag print print Dumper \%stuff; foreach my $id ( sort keys %stuff ) { #join this record by '|'. print join('|', $id, #turn inner arrays into comma separated via map. map { my %seen; #use grep to remove dupes - e.g. "abc,abc" -> "abc" join( ",", grep !$seen{$_}++, @$_ ) } @{ $stuff{$id} }{@header} ), "\n"; } __DATA__ id|Name|app1|app2 1|abc|234|231| 2|xyz|123|215| 1|abc|265|321| 3|asd|213|235|
As a result it'll generate:
id|Name|app1|app2 1|abc|234,265|231,321 2|xyz|123|215 3|asd|213|235
Therefore handles arbitrary fields and arbitrary numbers of 'collisions' neatly.
|
|---|