#!/usr/bin/perl use strict; use feature qw{ say }; use warnings; sub test { my ($array1ref, $array2ref) = @_; my @array1 = @$array1ref; my @array2 = @$array2ref; my $count = 0; foreach my $element (@array1) { foreach my $element2 (@array2) { if ($element eq $element2) { warn "$element $element2 same"; $count++; } else { warn "$element $element2 different"; } } } return $count } my $same = test([qw[ A T C G T C G A G C G ]], [qw[ A C G T C C T G T C G ]]); say "$same matches.";
Or, do you want to walk both the arrays at the same time? Then you can use indices:
sub test { my ($array1ref, $array2ref) = @_; my $count = 0; for my $idx (0 .. $#$array1ref) { my $element = $array1ref->[$idx]; my $element2 = $array2ref->[$idx]; if ($element eq $element2) { warn "$element $element2 same"; $count++; } else { warn "$element $element2 different"; } } return $count }
Update: You can also just use shift, but you shouldn't use it on the dereferences directly if you want to keep the original arguments to the function unchanged.
sub test { my ($array1ref, $array2ref) = @_; my @array1 = @$array1ref; my @array2 = @$array2ref; my $count = 0; for my $idx (0 .. $#array1) { $count++ if shift @array1 eq shift @array2; } return $count }
($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
In reply to Re: Array looping
by choroba
in thread Array looping
by Nansh
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |