foreach with two arrays

Discussion in 'Perl Misc' started by Thomas P., Jul 19, 2005.

  1. Thomas P.

    Thomas P. Guest

    Hello,

    I have two arrays and I want to use the foreach loop with both arrays
    simultaneously, that means the loop should take one value from both arrays
    at one run.
    I tried something like this but it does not work :
    @a=("12","14");
    @b=("7","9");
    foreach $value1 $value2 (@a,@b)

    Regards
    Thomas
     
    Thomas P., Jul 19, 2005
    #1
    1. Advertising

  2. Thomas P.

    Paul Lalli Guest

    Thomas P. wrote:
    > I have two arrays and I want to use the foreach loop with both arrays
    > simultaneously, that means the loop should take one value from both arrays
    > at one run.
    > I tried something like this but it does not work :
    > @a=("12","14");
    > @b=("7","9");
    > foreach $value1 $value2 (@a,@b)


    I do not believe you're going to find the functionality you're looking
    for in any built-in construct. But doing it manually really isn't that
    hard.

    #assumes @a and @b have same size.
    for my $i (0..$#a){
    my ($val1, $val2) = ($a[$i], $b[$i]);
    #...
    }

    Another option might be to build a hash from the two arrays, and use
    the each operator:

    #assumes @a contains only unique values
    #also assumes you don't care about the order or the elements,
    #only their relations
    my %hash;
    @hash{@a} = @b
    while (my ($val1, $val2) = each %hash){
    #...
    }

    Paul Lalli
     
    Paul Lalli, Jul 19, 2005
    #2
    1. Advertising

  3. Thomas P.

    Thomas P. Guest

    I just wanted to find out if this option is included in the foreach loop.
    Thanks for the example.


    "Paul Lalli" <> wrote in message
    news:...
    > Thomas P. wrote:
    > > I have two arrays and I want to use the foreach loop with both arrays
    > > simultaneously, that means the loop should take one value from both

    arrays
    > > at one run.
    > > I tried something like this but it does not work :
    > > @a=("12","14");
    > > @b=("7","9");
    > > foreach $value1 $value2 (@a,@b)

    >
    > I do not believe you're going to find the functionality you're looking
    > for in any built-in construct. But doing it manually really isn't that
    > hard.
    >
    > #assumes @a and @b have same size.
    > for my $i (0..$#a){
    > my ($val1, $val2) = ($a[$i], $b[$i]);
    > #...
    > }
    >
    > Another option might be to build a hash from the two arrays, and use
    > the each operator:
    >
    > #assumes @a contains only unique values
    > #also assumes you don't care about the order or the elements,
    > #only their relations
    > my %hash;
    > @hash{@a} = @b
    > while (my ($val1, $val2) = each %hash){
    > #...
    > }
    >
    > Paul Lalli
    >
     
    Thomas P., Jul 19, 2005
    #3
  4. * Paul Lalli schrieb:
    > Thomas P. wrote:
    >
    >> I have two arrays and I want to use the foreach loop with both arrays
    >> simultaneously, that means the loop should take one value from both arrays
    >> at one run.

    >
    > I do not believe you're going to find the functionality you're looking
    > for in any built-in construct.


    You're right. But each time you search for a special functionality not
    provided by Perl itself you could ask CPAN for helpful solutions. Some
    functions for processing lists are summarized in the modules List::Util
    and List::MoreUtils. To do something as described above you could use
    one of List::MoreUtils' functions: either pairwise() or each_array(),
    depends on what you really want to do.

    > But doing it manually really isn't that hard.


    Yes, it's not hard to implement this yourself, but I prefer not to
    reinvent the wheel. That's one reason I like Perl ;-)

    regards,
    fabian
     
    Fabian Pilkowski, Jul 19, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Philipp
    Replies:
    21
    Views:
    1,135
    Philipp
    Jan 20, 2009
  2. Kev Jackson
    Replies:
    2
    Views:
    115
  3. Joshua Muheim
    Replies:
    8
    Views:
    197
    Dejan Dimic
    Nov 23, 2007
  4. Allen Walker

    Merging two arrays -> array of arrays

    Allen Walker, May 21, 2010, in forum: Ruby
    Replies:
    6
    Views:
    168
    Jesús Gabriel y Galán
    May 21, 2010
  5. Thomas Becker

    Naming some arrays in a foreach loop?!

    Thomas Becker, Jul 7, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    86
    Benjamin Goldberg
    Jul 7, 2003
Loading...

Share This Page