Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to compare string?

Status
Not open for further replies.

michael12

Programmer
Sep 26, 2002
25
0
0
US
I have @a
foreach $a (@a) {print $a;}
the result is: a11,a12,a13....

also I have @b
foreach $b (@b) {print $b};
the result is: b11,b12,b13....

how can I set the condition to compare $a, $b, for example if a11=b11 then print "matched".
 
Is this what you want?
Code:
for ($i=0; $i<=$#a; $i++) {
    if ($a[$i] eq $b[$i]) {
        print &quot;$i matched \n&quot;;
    }
}
 
but how can use &quot;foreach&quot; at the same time?
do you mean:
foreach $a (@a) {
foreach $b (@b) {
for ($i=0; $i<=$#a; $i++) {
if ($a[$i] eq $b[$i]) {
print &quot;$i matched \n&quot;;
}
}

}

}
 
why do you want to insist on using foreach? Raider's for loop solution seems to do exactly what you want.
 
Hi

Try this out using &quot;foreach&quot;

foreach $a (@a) {
foreach $b (@b) {
if ($b eq $a) {
print &quot;$b Matched $a\n&quot;;
}
}
}

Rajeev
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top