in reply to Array looping

Your goal is not clear.

Your code with nested foreach loops is comparing every single element of the secnd array with every single element of the first array. Is this really what you want to do? It doesn't make too much sense to me with the input data.

May be you want to traverse arrays in parallel, i.e., report a match is the nth item of array 1 is equal to the nth element of array 2. At least, this would make more sense to me with your data. In that case, you should not use nested loops, but one loop looping over the indexes (subscripts) and comparing the items for each subscript.

Please clarify what you're trying to achieve.

Replies are listed 'Best First'.
Re^2: Array looping
by Anonymous Monk on Apr 12, 2017 at 17:36 UTC
    goal make work:
    my ($array1ref, $array2ref) = @_; my @array1 = @$array1ref; my @array2= @$array2ref; my $array1count = scalar @array1; my $array2count = scalar @array2; my $count = 0; for (my $array1index = 0; $array1index < $array1count; $array1index++) + { for (my $array2index = 0; $array2index < $array2count; $array2index++ +) { if ($array1[$array1index] eq $array2[$array2index]) { $count++; } } } print "$count"; print " matches found"; print $/;