How do I read two items at a time in a Perl foreach loop?

前端 未结 19 1425
你的背包
你的背包 2020-12-14 15:12

What I\'m looking for is something like:

@list = qw(1 2 3 4 5 6);
foreach (@list) {
  #perl magic goes here 
  print \"i: $i, j:$j\\n\";
}

相关标签:
19条回答
  • 2020-12-14 15:43

    I think more simpler way is to use old poor 'each'. Straight like this:

    while (my ($key,$value) = each @list) {
            print "$key=$value\n";
    }
    

    Updated:

    Yes, it's wrong. One should convert list to hash first but it could be too exensive:

    my %hash = (@list);
    while (my ($key,$value) = each %hash) {
            print "$key=$value\n";
    }
    
    0 讨论(0)
提交回复
热议问题