Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: join column 3 for unique values

by kevbot (Vicar)
on Sep 06, 2016 at 02:12 UTC ( [id://1171225]=note: print w/replies, xml ) Need Help??


in reply to join column 3 for unique values

Hi hello_beginner,

Here is another way to do it with Data::Table.

Space-delimited data is in data.txt

A1 abc yellow B1 xyz green A2 cde red A1 abc green A2 cde yellow A1 abc blue
Running 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']); my $ct = $mt->cast(['col1', 'col2'], 'variable', Data::Table::STRING, +'value', \&col_join); $ct->sort('col1', 1, 0); print $ct->csv(0, {delimiter => " "}); exit; sub col_join { my @data = @_; my $joined = join(":", @data); return($joined); }
gives the following output,
A1 abc yellow:green:blue A2 cde red:yellow B1 xyz green

Replies are listed 'Best First'.
Re^2: join column 3 for unique values
by hello_beginner (Novice) on Sep 06, 2016 at 04:25 UTC

    How do I print column 4 and col 5 data, if there are more then 3 columns</p

      I'm not sure that I understand your request. The script in my earlier reply (Re: join column 3 for unique values) will work with additional columns in the data file. For example, if data.txt contains this
      A1 abc yellow 1 abc B1 xyz green 2 def A2 cde red 3 ghi A1 abc green 4 jkl A2 cde yellow 5 mno A1 abc blue 6 pqr
      the output will be
      A1 abc yellow:green:blue 1:4:6 abc:jkl:pqr A2 cde red:yellow 3:5 ghi:mno B1 xyz green 2 def

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1171225]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (1)
As of 2024-04-25 05:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found