Let's take another approach:

What are you trying to achieve?

You want to know in what rows the letters (of the first row) occur? (although that's not quite correct) or ...?

As in, $answ_hoa1[1] (["B","B","-","B"]) tells you that the first row, the second row and the fourth row has the letter 'B'

But this is only true for $answ_hoa1[0] and $answ_hoa1[1]... But not for $answ_hoa1[3] and $answ_hoa1[4] probably because of rule 6, is this view correct?

Unless: if the match for the next letter may only start where the last one ended: example: it matches the B in the key2-array at the end, which means that it should start looking for the C and the D after the B (which is impossible since B is the end of the array)

Is the following input possible?:

'key1' => ["A","B","C","D"], 'key2' => ["A","C","B","C"], 'key3' => ["C","A","D","H"], 'key4' => ["A","B","I","C"],
And more important, what should the output of it be?
Should it be this?:
[ "A", "A", "A", "A" ], [ "B", "B", "-", "B" ], [ "C", "C", "-", "C" ], [ "D", "-", "D", "-" ],

Updated after reading tlm's code

Update2: code. This code does basiclly the same as tlm's code, except that his code modifies the hash (that is the arrays in the hash), and mine doesn't.

my @output = (); my @keys = sort keys %hash; my %start = (); @start{@keys} = (0) x scalar(@keys); my $first = shift @keys; my $total_columns = $# { $hash{$first} }; for my $column (0 .. $total_columns) { $output[$column] = [ $hash{$first}[$column] ]; for my $hkey (@keys) { my $aref = $output[$column]; push @$aref, "-"; for my $i ($start{$hkey} .. $total_columns) { if ($aref->[0] eq $hash{$hkey}[$i]) { $aref->[-1] = $aref->[0]; $start{$hkey} = $i + 1; last; } } } }

In reply to Re: Aligning Arrays Problem by Animator
in thread Aligning Arrays Problem by monkfan

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.