Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^2: Merging two array(refs)

by choroba (Canon)
on Jan 21, 2013 at 01:17 UTC ( #1014367=note: print w/ replies, xml ) Need Help??


in reply to Re: Merging two array(refs)
in thread Merging two array(refs)

Note that you replied to yourself instead of replying to me. Therefore, I did not notice your reply immediately.

Your code does not do what you claim. No arrays get merged when I run it. Please try to give more details: should the arrays be merged only if the last member is the same, or if any member is the same (i.e., should the script merge qw/a b key c/ with qw/x key y z/)? What would you do if more than one array from the second list could be merged into an array from the first one (i.e. both qw/car dog age/ and qw/train cat age/ in the second list)?

لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re^2: Merging two array(refs)
Select or Download Code
Re^3: Merging two array(refs)
by kpiti (Novice) on Jan 21, 2013 at 01:43 UTC
    I've been staring at the screen for too long..

    It's funny you say it doesn't merge, I've just copied the code from the post and used Data::Dumper to dump $arr at the end and I can see it merged as it should be..

    No, the initial position of the member doesn't matter. If we look at an arbitrary first row of two arrefs:

    $a->[0] = [qw/f0 f1 f2 f3/]; $b->[0] = [qw/e0 f2 e1 f0 e2];
    If the value of the field in $b->[0] matches one already present in $a->[0] then all values in the matching field in the $b->1..$ arrays should be set in the already matched field. So the final array would be like:
    $m = [ [qw/f0 f1 f2 f3 e0 e1 e2], [qw/a0 a1 a2 a3 "" "" ""/],# ..and so on all of $a [qw/b3 "" b1 "" b0 b2 b4/],# ..and so on for $b->[1..$]
    a0,1,2.. being the first,second,third element of a $a->[x] array and likewise for the b0,1,2 of the $b->[x] array.

    Does this make it any clearer?

      Oh, wait, now I understand. The first member of the array is the header! I can see now what you mean by "merge", I imagined something different, which did not happen. I apologize for offending your code. Here is how I would do it. How sexy it is depends on the beholder's perspective:
      #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my $arr = [[qw/name pos loc age/], [qw/ike boss 12 44/], [qw/mat slave 22 21/], [qw/jill sec 15 32/],]; my $add = [[qw/car dog age/], [qw/a1 grr 3/], [qw/s2 miew 7/],]; my $i = 0; my %headers; for (@{ $arr->[0] }, @{ $add->[0] }) { $headers{$_} = $i unless exists $headers{$_}; $i++; } my $result = [ [ sort { $headers{$a} <=> $headers{$b} } keys %headers +] ]; my $new_columns = keys(%headers) - @{ $arr->[0] }; for (1 .. $#{$arr}) { push @$result, [ @{ $arr->[$_] }, (q()) x $new_columns ]; } my @rearranged = map $headers{$_}, @{ $add->[0] }; for my $row (1 .. $#{$add}) { my @new = (q()) x keys %headers; my $i = 0; $new[$_] = $add->[$row][$i++] for @rearranged; push $result, [@new]; } print Dumper $result;
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        Thanks choroba, your code is definitely much clearer than mine. And yes, it is sexier =)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (15)
As of 2015-07-01 16:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (9 votes), past polls