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;
| [reply] [d/l] |
|
my %ip2host = (
ip1 => host1,
ip2 => host2,
ip3 => host3
);
| [reply] [d/l] |
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.
| [reply] [d/l] |
|
| [reply] |
Re: Join 2 arrays horizontally
by jwkrahn (Abbot) on Mar 03, 2019 at 23:52 UTC
|
use List::MoreUtils 'pairwise';
@array1 = qw(host1 host2 host3);
@array2 = qw(ip1 ip2 ip3);
my @result = pairwise { "$b $a" } @array1, @array2;
| [reply] [d/l] |
|
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: <%-{-{-{-<
| [reply] [d/l] [select] |
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]";
}
| [reply] [d/l] |
|
Thanks for your response, I have it working now.
| [reply] |
Re: Join 2 arrays horizontally
by rsFalse (Chaplain) on Mar 04, 2019 at 08:29 UTC
|
#!/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;
}
| [reply] [d/l] |
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
| [reply] [d/l] [select] |
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;
| [reply] [d/l] |
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. | [reply] |
|
| [reply] |