in reply to Arrays merges and redundancies
Update: changed code so that it works with non-numeric ID's
instead of printing, you can push a reference to \@new onto a new @Compressed array...
each_array() makes an iterator that pulls pairs of numbers walking left to right from the ID array and the Value array row that we are working on. uniq() removes duplicates (unique values only - order is preserved). see List::MoreUtils
Update Also tested with the other test case, the above code produces the correct result of:#!/usr/bin/perl -w use strict; use List::MoreUtils qw(each_array uniq); use Data::Dumper; my @ID = qw(1 1 2 3 4 4 6 6); my @Values = ([qw(a a a a a a a a )], [qw(b b b b b b b b )], [qw(c d c c c d c d )]); print join(" ",uniq @ID), "\n"; #compressed ID's foreach my $row_ref (@Values) { # make hash to gather up values for each ID # eg: 1 => [c, d] my %id2values; my $ea = each_array(@ID, @$row_ref); while ( my ($id, $value) = $ea->() ) { push @{$id2values{$id}}, $value; } # compress out the dupe values then make a "c/d" string # if there is more than one value my @new = map{join ("/", uniq @{$id2values{$_}})} uniq @ID; print "@new\n"; #compressed values } __END__ 1 2 3 4 6 a a a a a b b b b b c/d c c c/d c/d
Apple Grape Banana 5 2 3/4 10/15 3 4 for: my @ID = qw(Apple Apple Grape Banana Banana); my @Values = ([qw(5 5 2 3 4 )], [qw(10 15 3 4 4 )], );
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Arrays merges and redundancies
by remiah (Hermit) on Mar 31, 2012 at 02:54 UTC | |
by Marshall (Canon) on Mar 31, 2012 at 04:24 UTC | |
by remiah (Hermit) on Mar 31, 2012 at 13:45 UTC | |
by Anonymous Monk on Mar 31, 2012 at 10:27 UTC | |
by Anonymous Monk on Mar 31, 2012 at 10:49 UTC |