Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re^4: Merging two array(refs)

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

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1014371]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2018-05-27 05:36 GMT
Find Nodes?
    Voting Booth?