Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Comparing and Aligning arrays

by kcott (Abbot)
on Apr 04, 2014 at 07:25 UTC ( #1081078=note: print w/ replies, xml ) Need Help??


in reply to Comparing and Aligning arrays

G'day abd,

Welcome to the monastery.

As others have pointed out, your requirements are unclear and, as you've shown all data as paragraph text, the output format cannot be determined.

Also note that "It's very important for me to keep the original order." does not seem to match your required output: 1st input row is a a b and 1st output row is a a - (only the first two characters are in the same order); 1st input column is a b c and 1st output column is a b c - (only the first three characters are in the same order). You need to clarify what "original order" you are referring to.

Assuming both instances of "List1 List2 List3" are part of the description, i.e. not part of input or output data, then the following code transforms your provided input to your wanted output.

#!/usr/bin/env perl use strict; use warnings; my %list = (1 => [qw{a b c}], 2 => [qw{a b d}], 3 => [qw{b c d}]); my (%align, %seen); %{$align{$_}} = map { ++$seen{$_}; $_ => undef } @{$list{$_}} for keys + %list; for my $char (sort keys %seen) { for my $list (sort keys %align) { print exists $align{$list}{$char} ? $char : '-', ' '; } print "\n"; }

Output:

a a - b b b c - c - d d

[Please read the guidelines in "How do I post a question effectively?" and follow them in any future postings.]

-- Ken


Comment on Re: Comparing and Aligning arrays
Select or Download Code
Re^2: Comparing and Aligning arrays
by abd (Initiate) on Apr 04, 2014 at 12:58 UTC

    Dear Ken & All

    Please accept my apology for the weak explanation of what I need. This is my first question on perlmonks. I'll try to follow the instractions you sent for my next question.

    The code you drafted can solve my problem, I highly appreciate you efforts.

    Cheers

    Abd

Re^2: Comparing and Aligning arrays
by AnomalousMonk (Abbot) on Apr 04, 2014 at 19:55 UTC

    Oooooh... So you're saying that  List1 List2 List3 are actually headers and the list items descend below each header. So the data should really look something like
    List1 List2 List3
      a     a     b
      b     b     c
      c     d     d
    translating (to pseudocode) as
    List1 = [qw{a b c}];  List2 = [qw{a b d}];  List3 = [qw{b c d}];

    That's a much better guess than mine, and from the tone of abd's reply may even be right. When I first saw your post, I thought you'd lost your mind, but I've switched to ++.

      "Oooooh... So you're saying that List1 List2 List3 are actually headers ..."

      At first, I did read the rows as being the lists and struggled to work out how the transformation was supposed to be accomplished.

      I don't recall the exact thought process: I think three "ListN" in both input and input matching three columns in both (but not four rows in the output) probably led to the guess I made.

      "When I first saw your post, I thought you'd lost your mind, but I've switched to ++."

      Thanks for generating a hearty chuckle at my end: a good start to my day. :-)

      -- Ken

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (9)
As of 2014-12-22 23:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (133 votes), past polls