Paul Lalli wrote:
On Feb 26, 11:07 am, [EMAIL PROTECTED] (Irfan Sayed) wrote:
Hello All,
I have two arrays contains exact no. of elements. Now what I need to do
is , I want to execute certain commands to each elements of the array at
a time.
It means that I want take first element of first array and first element
of second array and then want to execute certain commands considering
these two elements.
I don't know how should I achieve this in perl.
In addition to the answers already received, you could take advantage
of the List::MoreUtils module from CPAN:
#!/usr/bin/perl
use strict;
use warnings;
use List::MoreUtils qw/zip natatime/;
my @foo = (1, 2, 3);
my @bar = qw/a b c/;
my $it = natatime 2, zip(@foo, @bar);
while (my ($f, $b) = $it->()) {
print "$f - $b";
}
__END__
1 - a
2 - b
3 - c
If we're buying into List::MoreUtils then I feel it's more explicit to
use each_array to create the iterator. You're also missing a newline in
your print statement.
Rob
use strict;
use warnings;
use List::MoreUtils qw/each_array/;
my @foo = 1 .. 3;
my @bar = 'a' .. 'c';
my $it = each_array(@foo, @bar);
while (my ($f, $b) = $it->()) {
print "$f - $b\n";
}
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/