Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Join 2 arrays horizontally

by perl_5eeker (Novice)
on Mar 03, 2019 at 23:23 UTC ( [id://1230807]=perlquestion: print w/replies, xml ) Need Help??

perl_5eeker has asked for the wisdom of the Perl Monks concerning the following question:

Hello (an obvious newbie), made good progress in the past month or so learning perl ;)... need help with an array question please array1 consists of multiple hostnames and array2 has corresponding IPs, I need to join the 2 arrays horizontally like element1 of array1 with element1 of array2 and so on.. (example below)

@array1 = qw(host1 host2 host3); @array2 = qw(ip1 ip2 ip3); # I want the output to look like below ip1 host1 ip2 host2 ip3 host3
I did find some solutions in the library but it seems I am not getting it right, can I get some help please? Thanks

Replies are listed 'Best First'.
Re: Join 2 arrays horizontally
by tybalt89 (Monsignor) on Mar 03, 2019 at 23:45 UTC
    #!/usr/bin/perl # https://perlmonks.org/?node_id=1230807 use strict; use warnings; my @array1 = qw(host1 host2 host3); my @array2 = qw(ip1 ip2 ip3); my %ip2host; @ip2host{@array2} = @array1; print "$_ $ip2host{$_}\n" for @array2;

      And depending on how the original data is sourced you may be better off simply creating the hash in the first place.

      my %ip2host = ( ip1 => host1, ip2 => host2, ip3 => host3 );
Re: Join 2 arrays horizontally
by 1nickt (Canon) on Mar 03, 2019 at 23:36 UTC

    Hi there, it would be good to see what you have tried.

    If you know the arrays are always going to have an equal number of elements, you can use the length to create a numeric range then loop through that range, using the numbers as indexes to get an element from each array.

    for my $i ( 0 .. $#array1 ) { say join ' ', $array2[$i], $array1[$i]; }

    There are a plethora of other ways, many more idiomatic. But this should get you started figuring out the concepts.

    Hope that helps!


    The way forward always starts with a minimal test.

      Awesome, thanks for the help - got me going!!

Re: Join 2 arrays horizontally
by jwkrahn (Abbot) on Mar 03, 2019 at 23:52 UTC

    Try List::MoreUtils pairwise function: (UNTESTED)

    use List::MoreUtils 'pairwise'; @array1 = qw(host1 host2 host3); @array2 = qw(ip1 ip2 ip3); my @result = pairwise { "$b $a" } @array1, @array2;

      I was thinking  each_array() myself. Assume all arrays are the same size:

      c:\@Work\Perl\monks>perl -wMstrict -le "use List::MoreUtils qw(each_array); ;; my @array1 = qw(host1 host2 host3); my @array2 = qw(ip1 ip2 ip3); my $ea = each_array(@array2, @array1); ;; while (my @ra = $ea->()) { print qq{:@ra:}; } " :ip1 host1: :ip2 host2: :ip3 host3:
      See List::MoreUtils.


      Give a man a fish:  <%-{-{-{-<

Re: Join 2 arrays horizontally
by ikegami (Patriarch) on Mar 03, 2019 at 23:37 UTC

    When dealing with parallel arrays, you will need to iterate over the indexes of one of the arrays rather than iterating over the array itself.

    for my $i (0..$#array1) { say "$array2[$i] $array1[$i]"; }
      Thanks for your response, I have it working now.
Re: Join 2 arrays horizontally
by rsFalse (Chaplain) on Mar 04, 2019 at 08:29 UTC
    Some more variants:
    #!/usr/bin/perl use strict; use warnings; my @array1 = qw(host1 host2 host3); my @array2 = qw(ip1 ip2 ip3); my $length1 = scalar @array1; # Asking for 2 elements of a list which is composed of elements from 2 + arrays print map "$_\n", map { join ': ', ( @array1, @array2 )[ $_, $_ + $length1 ] } 0 .. $length1 - 1; # Asking for elements of 2 arrays by using references for my $i ( 0 .. $length1 - 1 ){ print map "$_,\n", join ' --> ', map { $_->[ $i ] } \@array1, \@ar +ray2; }
Re: Join 2 arrays horizontally
by karlgoethebier (Abbot) on Mar 04, 2019 at 10:17 UTC

    use List::MoreUtils qw (mesh); my @stuff = mesh @hosts, @IPs;. If you like you can call it zip instead. Variatio delectat. Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help

Re: Join 2 arrays horizontally
by hdb (Monsignor) on Mar 04, 2019 at 13:04 UTC

    Just for fun: a solution that is probably wasteful and I am not even sure it will always work (as a minimum both arrays have to be of the same length).

    splice @array1, @array2, 0, pop @array2 while @array2;
Re: Join 2 arrays horizontally
by rsFalse (Chaplain) on Mar 04, 2019 at 07:35 UTC
    It is a convolution function, usually named as zip, which completes your task. Perl5 do not have the 'zip' function in its core, as do other similar level languages.

      But there is List::Zip.


      Enjoy, Have FUN! H.Merijn

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1230807]
Approved by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-19 23:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found