hello_beginner has asked for the wisdom of the Perl Monks concerning the following question:
Hi, I am trying to get the below output by reading rows from table. Need to join values of column 3 for unique column 1 values
Read input data
A1 text1 NY Jan 01 A2 text2 LN Feb 02 A3 text3 SG Mar 03 A2 text2 NY Feb 02 A1 text1 SG Jan 01 A1 text1 AUS Jan 01
Expected result
A1 text1 NY:SG:AUS Jan 01 A2 text2 LN:NY Feb 02 A3 text3 SG Mar 03
For unique column 1 values, column2, 4 and 5 will unique.
use warnings; use strict; my %hash; while (<DATA>){ chomp; my ($key, $col2, $col3) = split; push @{ $hash{$key}{$col2} }, $col3; } for my $k (keys %hash){ for (keys %{ $hash{$k} }){ my $cols3 = join ':', @{ $hash{$k}{$_} }; print "$k $_ $cols3\n"; } }
|
---|
Replies are listed 'Best First'. | |
---|---|
Joining columns
by hippo (Archbishop) on Sep 09, 2016 at 21:52 UTC | |
Re: Perl help
by kevbot (Vicar) on Sep 10, 2016 at 03:32 UTC | |
by hello_beginner (Novice) on Sep 10, 2016 at 05:10 UTC | |
by Marshall (Canon) on Sep 10, 2016 at 05:35 UTC | |
by kevbot (Vicar) on Sep 10, 2016 at 05:58 UTC | |
Re: Perl help
by kcott (Archbishop) on Sep 10, 2016 at 14:54 UTC | |
Re: Perl help
by neilwatson (Priest) on Sep 09, 2016 at 18:47 UTC | |
Re: Perl help
by stevieb (Canon) on Sep 09, 2016 at 18:18 UTC |