in reply to Merging two array(refs)
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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Merging two array(refs)
by choroba (Cardinal) on Jan 21, 2013 at 01:17 UTC | |
by kpiti (Novice) on Jan 21, 2013 at 01:43 UTC | |
by choroba (Cardinal) on Jan 21, 2013 at 01:59 UTC | |
by kpiti (Novice) on Jan 21, 2013 at 07:44 UTC |