Friday, November 12, 2010

How to merge two arrays at Perl


Share at Facebook

Its easy to join two arrays at Perl to produce a new one. Just like other smaller tasks, for joining, you'll not require any function to aid you. Just a comma(,) can do the task for you.



$\ = $/;

my @array_one = (1, 3, 5, 7);
my @array_two = (2, 4, 6, 8);

my @new_array = (@array_one, @array_two);
print for @new_array;



This code will join the two arrays, and will print the new one. Joining is done using a comma. You can pass multiple arrays are separated with comma. Pretty easy.




5 comments:

Andreas said...

what on earth are you trying to achieve by posting this?

in fact you are adding confusion by leaving the aenigmatic '$\ = $/' unexplained.

What a vain idiot, go and facebook and leave programming alone.

Demon said...

A fatass like you shouldn't leave comment on my blog... I have approved your comment so that other people know how fat your ass is...

Anonymous said...

lol at your comments, nevertheless the post is useful. The solution might be simple, but not until you know it.
Thanks.

UncleUnctuous said...

Love the comments about fat asses. Thanks for the smile!

Anonymous said...

The post is wrong-titled. The solution is not merge - as in title -, but join.

Merge means:
$a=[undef,undef, 1,2,undef,4]
$b=[undef,0, undef,2,3,5,6]

$c = merge_right($a, $b )

May result - depending on the merge policy -

undef,0,1,2,3,5,6 or
undef,0,1,2,3,4,6

Joining two lists is a trivia in Perl. You could have also make a post on how to join two strings. Definitely simpler as in C....