[Solved] 'Restart' keyword

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
es_91
Enthusiast
Enthusiast
Posts: 298
Joined: Thu Jan 27, 2011 12:00 pm
Location: DE

[Solved] 'Restart' keyword

Post by es_91 »

Hello.

I just programmed this...

Code: Select all

Repeat
  
  Found = #False
  
  ForEach Names$ ()
    If Names$ () = Name$
      Name$ + "_"
      Found = #True
      Break
    EndIf
  Next
  
Until Not Found
...but it would be so much quicker like this...

Code: Select all

ForEach Names$ ()
  If Names$ () = Name$
    Name$ + "_"
    Restart
  EndIf
Next
What do you think?

-------------------

Here's the above code in a testable example:

Code: Select all

NewList Names$ ()

AddElement (Names$ ())
Names$ () = "Darin"

AddElement (Names$ ())
Names$ () = "Claude"

AddElement (Names$ ())
Names$ () = "Claude"

AddElement (Names$ ())
Names$ () = "Enrico"

Name$ = "Claude"

Repeat
  
  Found = #False
  
  ForEach Names$ ()
    If Names$ () = Name$
      Name$ + "_"
      Found = #True
      Break
    EndIf
  Next
  
Until Not Found

AddElement (Names$ ())
Names$ () = Name$

ForEach Names$ ()
  
  Debug Names$ ()
  
Next
Last edited by es_91 on Mon Dec 01, 2014 8:59 pm, edited 1 time in total.
:mrgreen:
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: 'Restart' keyword

Post by IdeasVacuum »

In your example, there is only one name to match and so once that name is found there is no need to restart the loop. If instead your code was comparing two lists, you would use 2x ForEach and there would still be no need to restart. So, do you have an example where Restart would be invaluable?
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
es_91
Enthusiast
Enthusiast
Posts: 298
Joined: Thu Jan 27, 2011 12:00 pm
Location: DE

Re: 'Restart' keyword

Post by es_91 »

I made a mistake on that example.

I edited the above code to show the effect wanted.
:mrgreen:
es_91
Enthusiast
Enthusiast
Posts: 298
Joined: Thu Jan 27, 2011 12:00 pm
Location: DE

Re: 'Restart' keyword

Post by es_91 »

Ah, I made so much errorious stuff up there, and, as IdeasVacuum said, I was wrong ... the desired job can be done with Continue:

Code: Select all

NewList Names$ ()

AddElement (Names$ ())
Names$ () = "Darin"

AddElement (Names$ ())
Names$ () = "Claude"

AddElement (Names$ ())
Names$ () = "Enrico"

AddElement (Names$ ())
Names$ () = "Claude"

AddElement (Names$ ())
Names$ () = "Enrico"

AddElement (Names$ ())
Names$ () = "Claude"

NewList NewNames$ ()

ForEach Names$ ()
  
  ForEach NewNames$ ()
    
    If NewNames$ () = Names$ ()
      
      Names$ () + "_"
      
      Continue
      
    EndIf
    
  Next
  
  AddElement (NewNames$ ())
  NewNames$ () = Names$ ()
  
Next

ForEach NewNames$ ()
 
  Debug NewNames$ ()
 
Next
I can now, after 30 minutes of stressing and confusing tests with an imaginary power that seemingly does not exists, not even say if there is a use for a Restart keyword.

Sorry for this thread.
:mrgreen:
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2137
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: 'Restart' keyword

Post by Andre »

Ok, so I will mark this thread as 'Solved' :D
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
Post Reply