Here is one way...using use List::MoreUtils qw(each_array uniq);...Uses a hash of array (HoA) to gather up the values associated with the ID, then that hash is accessed in the same order as the ID array and values are compressed, re-formatted.

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

#!/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
Update Also tested with the other test case, the above code produces the correct result of:
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 )], );

In reply to Re: Arrays merges and redundancies by Marshall
in thread Arrays merges and redundancies by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.