Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Perl help

by kevbot (Priest)
on Sep 10, 2016 at 03:32 UTC ( #1171497=note: print w/replies, xml ) Need Help??

in reply to Perl help

Hi hello_beginner,

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

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
This script,
#!/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); }
will give this output
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

    Thanks for your help. I am reading the data from database and not csv.

      Then just adapt the SQLite code I gave you at: Re: join column 3 for unique values.
      Read the whole previous thread carefully, there are many ideas in there that are applicable to this problem which is almost the same one.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1171497]
and a kettle whistles...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2018-06-18 08:08 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (109 votes). Check out past polls.