in reply to Perl help
I agree with hippo, this question is very similar to join column 3 for unique values. In fact, a few changes to the code I posted in Re: join column 3 for unique values gives the results that you require.
If you have your data in data.txt
This script,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
will give this output#!/usr/bin/env perl use strict; use warnings; use Data::Table; my $t = Data::Table::fromCSV('data.txt', 0, undef,{delimiter => ' '}); my $mt = $t->melt(['col1', 'col2', 'col4', 'col5']); my $ct = $mt->cast(['col1', 'col2', 'col4', 'col5'], 'variable', Data: +:Table::STRING, 'value', \&col_join); $ct->sort('col1', 1, 0); $ct->reorder( [ 'col1', 'col2', 'col3', 'col4', 'col5' ] ); print $ct->csv(0, {delimiter => " "}); exit; sub col_join { my @data = @_; my $joined = join(":", @data); return($joined); }
A1 text1 NY:SG:AUS Jan 01 A2 text2 LN:NY Feb 02 A3 text3 SG Mar 03
If you often work on problems like these, I recommend reading Data::Table and the Data::Table Cookbook (especially Section 8.1: Reshaping - melting and casting for table statistics).
As mentioned by stevieb, please take a closer look at How do I post a question effectively? especially the Select an informative title section.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl help
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 |