Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 12-22-2010, 07:57 PM PHP While Loops
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
I'm not the best looper here on WT, so I have this question for those of you who are

Let's say I have an array that's taking randomly generated words and checking if they are the word given. So say we have any 4-letter combination of any character, and we are checking if it says 'asdf'. This could take forever, and it is definitely possible. There are 1500625 (?) different combinations to this (1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ) in a four-character sequence.

Basically what I'm trying to say (and failing) is how can I actively and efficiently run huge loops without it lagging or having to wait for the loop to complete before displaying anything?


We could say I wanted to run 5000000 iterations to check if x (which is a random string) is equal to "asdf". That'll take a bit. But what if the program finds a match on the 2723'rd iteration? We'd have to wait until it completes the entire loop before we find everything. I need to make the loop stop when that happens.

Thanks

-PG
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!

Last edited by Physicsguy; 12-22-2010 at 08:02 PM..
Physicsguy is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 12-22-2010, 08:12 PM Re: PHP While Loops
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Never mind, I found a solution.

Just place 'break;' when you find a solution.
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 12-22-2010, 09:58 PM Re: PHP While Loops
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by Physicsguy View Post
Never mind, I found a solution.

Just place 'break;' when you find a solution.
NOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO

Sorry that was just my initial reaction when read your solution (think person fall off of a cliff or diving in front of a bullet)

Using break outside of a switch statement isn't good form. If you do it often enough your code will become difficult to follow and more unpredictable.

What you should do is specify a terminating condition in the conditions in your loop. A linear search for example:
PHP Code:
function my_in_array($needle$haystack)
{
     
$found false;
     while(
count($haystack) != && !$found)
     {
          if(
$needle == array_shift($haystack))
               
$found true;
     }

     return 
$found;

__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 12-22-2010 at 10:00 PM..
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 12-22-2010, 10:02 PM Re: PHP While Loops
Physicsguy's Avatar
404 - Title not found

Posts: 920
Name: Scott Kaye
Location: Ontario
Trades: 0
Ah. So I see. Thank you, Null :P
__________________
Check out my
Please login or register to view this content. Registration is FREE
or my
Please login or register to view this content. Registration is FREE
!
Physicsguy is offline
Reply With Quote
View Public Profile
 
Old 12-23-2010, 07:42 PM Re: PHP While Loops
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Why not use:

if (in_array($needle, $haystack))...
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 12-23-2010, 07:44 PM Re: PHP While Loops
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by mgraphic View Post
Why not use:

if (in_array($needle, $haystack))...
I was implementing in_array for the sake of example
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 12-24-2010, 04:28 AM Re: PHP While Loops
Novice Talker

Posts: 5
Trades: 0
Is using Break; just bad practice or does it cause functionality issues? If there's multiple nested loops, it would be easier using break; than having variables like $found, $found1, $found2.
Atlos is offline
Reply With Quote
View Public Profile
 
Old 12-24-2010, 06:15 AM Re: PHP While Loops
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
To be fair there are different schools of thought regarding break.

I discourage using break purely from a design perspective. I would argue that instead of using variables like $found $found1 $found2.. etc you should use variables with meaningful names that will lend some insight into what is going on to another programmer.

In general I think interrupting the control flow of your program makes it easier to introduce bugs, especially when modifying your code. Here is a simple example:

PHP Code:
$i 0;
while(
$i 10)
{
     if(
$i == 5)
          continue;

     
$i++;

This example is a little trivial, but in a real world implementation you might not realize that code similar to that above is actually an infinite loop.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Reply     « Reply to PHP While Loops
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.27111 seconds with 12 queries