Times

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Times

Post by netmaestro »

From the help file:

Code: Select all

 
a=0
Repeat 
  a=a+1
Until a>100
; This will loop until "a" takes a value > to 100, (it will loop 101 times). 
Rather than define and increment a counter if I want to execute something a specific number of times this would be handy:

Code: Select all

Repeat
   ; some lines of code
5 Times
Not exactly a game-changer but when you're doing a lot of quick tests it would save some time. More streamlined than For-Next and nicely self-documenting.
BERESHEIT
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Times

Post by luis »

I can think to an overwhelming number of possible scenarios suggesting the need of a loop indexed in some way, but I have an hard time to find more than some where you want just to loop blindly without a counter of some sort instead.
And after all the generated code still will have the counter, so... meh :)
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
ostapas
Enthusiast
Enthusiast
Posts: 192
Joined: Thu Feb 18, 2010 11:10 pm

Re: Times

Post by ostapas »

+1, looks simple and laconic and there may be not so few situations where you don't need loop index.
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Times

Post by Little John »

Here is a self-made solution. :-)

Code: Select all

EnableExplicit

Macro DoTimes (_times_)
   CompilerIf Not Defined(DoTimesRepeatCount, #PB_Variable)
      Define DoTimesRepeatCount.i
   CompilerEndIf
   For DoTimesRepeatCount = 1 To _times_
EndMacro


DoTimes(3)
   Debug "Hi"
Next

DoTimes(3)
   Debug "there!"
Next
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Times

Post by STARGÅTE »

for recursive support:

Code: Select all

Macro DoTimes (_times_)
   Define DoTimesRepeatCount#MacroExpandedCount.i
   For DoTimesRepeatCount#MacroExpandedCount = 1 To _times_
EndMacro


DoTimes(3)
	DoTimes(3)
	   Debug "there!"
	Next
Next
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Times

Post by PB »

> Repeat
> ; some lines of code
> 5 Times

A quick macro which is close to your specific syntax request:

Code: Select all

Global timescount

Macro Times(num)
  timescount+1
  Until timescount=num
  timescount=0
EndMacro

Repeat
  Debug "Pure"
Times(5)

Repeat
  Debug "Basic"
Times(5)
But syntax coloring doesn't match, and it doesn't support nesting. TIOLI. :lol:
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Times

Post by davido »

Thanks Guys. Some nice ideas.

Hi PB,

Sorry I can't help with the nesting.

But you can get the colour correct by making Times a custom variable and making the custom variable the same colour as system variables.
DE AA EB
Little John
Addict
Addict
Posts: 4779
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Times

Post by Little John »

Cool, STARGÅTE. 8)
Post Reply