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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi I have this piece of code that checks every single pair of useri

Status
Not open for further replies.

fortytwo

Technical User
Apr 18, 2000
206
GB
Hi<br><br>I have this piece of code that checks every single pair of userid's and passwoard in an array, however even if the userID is found in the first line, it will still go on to check the whole of the rest of the array.<br><br>foreach $user (@users){<br>$_ = $user;<br>if ( /$userid\#(.*?)\#/ )<br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;$password = $1;<br>&nbsp;&nbsp;}<br>else<br>&nbsp;&nbsp;{<br>&nbsp;&nbsp;$nouser = 1;<br>&nbsp;&nbsp;}<br>}<br><br>Is there anyway of terminating the serarch (exiting the loop) when the userID is found?<br><br>Thanks<br>Will<br> <p>fortytwo<br><a href=mailto:will@hellacool.co.uk>will@hellacool.co.uk</a><br><a href= test site</a><br>
 
You are not doing anything to break the loop.&nbsp;&nbsp;You can use '<i>last</i>' to break the loop once you find what you want<br>....like this.....<br><br><b><br>#!/usr/local/bin/perl<br>$num = 3;<br>foreach $tmp (0..6)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;print &quot;Tmp $tmp\n&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;if ($tmp != $num) { next; }<br>&nbsp;&nbsp;&nbsp;&nbsp;if ($tmp == $num) { last; }<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br></b>
 
Thanks, for that I will give it a go.&nbsp;&nbsp;Sorry about the spelling of my post, we had beers in the office :) <p>fortytwo<br><a href=mailto:will@hellacool.co.uk>will@hellacool.co.uk</a><br><a href= test site</a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top