Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge two arrays, alternating values from each array, in perl

Tags:

arrays

perl

Suppose I have 2 arrays like mentioned below

@a1 = ("Vinay", Raj, harry);
@b1 = ("dude","rock");

After merging I want to have result like this

[
    Vinay
    dude
    Vinay
    rock
    Raj
    dude
    Raj
    rock
    harry
    dude
    harry
    rock
]

basically I want to merge the each index values of array1 to all the index values of array2.

Adding to the above question I have another query.

For the same question above how to merge 2 arrays at particular array index. For example I have 2 arrays of each 160 elements, now I want to merge array at every 5th element in sets, is that possible?

like image 378
Vinay D Avatar asked Oct 28 '12 06:10

Vinay D


People also ask

How do I append two arrays in Perl?

Concatenating Arrays in PerlUse the built-in push() function to add one array to the end of another.

How do I combine two arrays into a new array?

The concat() method concatenates (joins) two or more arrays. The concat() method returns a new array, containing the joined arrays. The concat() method does not change the existing arrays.

How do I join an array element in Perl?

Perl Array join() FunctionThe Perl programming language join() function is used to connect all the elements of a specific list or array into a single string using a specified joining expression. The list is concatenated into one string with the specified joining element contained between each item.

Is there a way to merge two arrays in Perl?

Here is one way to do it. May not be the best perl syntax, though. The double for loop means you will be pushing every combination of the two arrays into the merged array. I've tried to set it up so that the result is in the order you specified in the question.

How to combine two arrays with alternate elements?

Given two arrays arr1 [] and arr2 [], we need to combine two arrays in such a way that the combined array has alternate elements of both. If one array has extra element, then these elements are appended at the end of the combined array. Recommended: Please try your approach on {IDE} first, before moving on to the solution.

How to merge two sorted arrays with O (1) extra space?

This step take O (n1 * n2) time. We have discussed implementation of above method in Merge two sorted arrays with O (1) extra space. Method 2 (O (n1 + n2) Time and O (n1 + n2) Extra Space) The idea is to use Merge function of Merge sort . Create an array arr3 [] of size n1 + n2. Simultaneously traverse arr1 [] and arr2 [].

What is the difference between join () and split () function in Perl?

This join () function in Perl works completely opposite to the split () function as this breaks the given single string into an array of elements along with specified separators in Perl.


1 Answers

Just make a new array:

my @merged = (@a1, @b1);

Here's a complete example:

my @a1 = ("foo", "bar");
my @a2 = ("baz", "spam");

my @merged = (@a1, @a2);

print $merged[3]; #=> "spam"

EDIT: I missed the ordering requirement. You just need to zip them together, which you can do with List::MoreUtils:

use List::MoreUtils qw(zip);
use Data::Dumper qw(Dumper);

my @a1 = ("Vinay", "Raj", "harry");
my @a2 = ("dude", "rock");

my @merged = zip(@a1, @a2);

print Dumper(\@merged);
like image 121
jmdeldin Avatar answered Nov 08 '22 23:11

jmdeldin