Page 1 of 1

How can I do this while loop in 1 line

Posted: 15 Feb 2018, 16:27
by DigiDon
Hi,

Seems like a simple question but I wasn't able to come with the right syntax.

I would like something like
while the regexmatch is not null and <= LengthLim, store it in TempFoundDelimPos and do ...

Code: Select all

while (TempFoundDelimPos:=regexmatch(TextToWrapNew,P_RegexDelim,,CurrPos)<=LengthLim) {
			...
			}
Instead of writing

Code: Select all

while (regexmatch(TextToWrapNew,P_RegexDelim,,CurrPos) and regexmatch(TextToWrapNew,P_RegexDelim,,CurrPos)<=LengthLim) {
			TempFoundDelimPos:=regexmatch(TextToWrapNew,P_RegexDelim,,CurrPos)
			...
			}
Which is long, redundant and performs 3 same regexmatch

Thanks ! ;)

Re: How can I do this while loop in 1 line

Posted: 15 Feb 2018, 16:48
by Exaskryz
I do this set up

Code: Select all

pos:=1
while (pos<=LengthLim) && (pos>0) ; it returns 0 when not found, not sure if you could use ErrorLevel here
{
pos:=regexmatch(haystack,needle)
do stuff
}
This does assume that you will find a result each time. You could work around that with this logic actually:

Code: Select all

pos:=regexmatch(haystack,needle)
while (pos<=LengthLim) && (pos>0)
{
do stuff
pos:=regexmatch(haystack,needle)
}
This way, the while loop is only ever entered if pos returned a good result for you. Once it's in the while loop, the last thing it does is updates pos, which will make the while expression evaluate to see if it should repeat itself or move beyond the loop.

Re: How can I do this while loop in 1 line

Posted: 15 Feb 2018, 19:14
by DigiDon
Right, seems simple that way :)
I thought I was missing the syntax to set it up directly but I guess that means it is not so easy.
Anyway, the trick seems fine, many thanks !