in reply to join column 3 for unique values

I'd stash everything into a data structure (HoHoA, ie. hash of hashes of arrays), which makes sure everything gets aggregated properly, then print it out.

use warnings; use strict; my %hash; while (<DATA>){ chomp; my ($key, $val, $colour) = split; push @{ $hash{$key}{$val} }, $colour; } for my $k (keys %hash){ for (keys %{ $hash{$k} }){ my $colours = join ':', @{ $hash{$k}{$_} }; print "$k $_ $colours\n"; } } __DATA__ A1 abc yellow B1 xyz green A2 cde red A1 abc green A2 cde yellow A1 abc blue

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 05, 2016 at 15:04 UTC

    Great, this works.

    If there is duplicate value for A1, like below Yellow, it reapts. we need i to print only once </>

    __DATA__

    A1 abc yellow

    A1 abc yellow

    A2 cde yellow

    A1 abc blue

    Output:

    A1 abc yellow:yellow:blue ---only yellow:blue

    A2 cde yellow

    B1 xyz green

      You'll need to show a little effort here now. Hint: check the array if the colour already exists before pushing it onto itself, perldoc -f grep, or use a hash for the colours as well, if the order doesn't matter.

      update: Also, please see How do I post a question effectively? for the use of <code></code> tags (they are for code *and* data), and use the Preview button to ensure all of your tags are closed etc.