Let me try to be more clear on what I mean:

in the following arrays qw/name pos loc age/ and qw/car dog age/ the "age" field is present in the first array already, so I don't want to add it again, I want to add the in the "age" (=last) position in the second array to the already existing "age" position in the final combined arref..

I hope that explains it a bit.

I can post the code I built up which doesn't look nice, though it does the job.. I'd prefer to have something nicer if possible

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 ($arrfld,$addfld); for ($i=0;$i<@{$arr->[0]};$i++){ $arrfld->{ $arr->[0]->[$i] } = $i } for ($i=0;$i<@{$add->[0]};$i++){ unless ( $arrfld->{ $add->[0]->[$i] } ){ $arrfld->{ $add->[0]->[$i] } = scalar(keys %$arrfld); } $addfld->{ $arrfld->{ $add->[0]->[$i] } } = $i; } foreach my $row (@$arr){ for($x=scalar(@$row);$x<scalar(keys %$arrfld);$x++){push @$row,""} } shift @$add; foreach my $row (@$add){ my $nr; for ($x=0;$x<scalar(keys %$arrfld);$x++){ $nr->[$x] = defined($addfld->{$x})?$row->[$addfld->{$x}]:""; } push @$arr,$nr; }

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

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.