Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^4: Merging two array(refs)

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


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

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;
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re^4: Merging two array(refs)
Download Code
Re^5: Merging two array(refs)
by kpiti (Novice) on Jan 21, 2013 at 07:44 UTC
    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://1014371]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (13)
As of 2014-12-22 18:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (126 votes), past polls