Page 1 of 1

FOR-Loop with list numbers

Posted: Tue Oct 24, 2017 10:18 am
by Lebostein
I like the list loops in Python. Would be nice to can to that:

Code: Select all

For i = 1,2,7,8,-5,100
...
Next i

Re: FOR-Loop with list numbers

Posted: Tue Oct 24, 2017 12:19 pm
by RSBasic
+1

Re: FOR-Loop with list numbers

Posted: Tue Oct 24, 2017 7:06 pm
by infratec
Hm...

why not use a List() and ForEach ?
I see no real reason for the 'other' syntax.

Maybe an AddMultipleElements() makes more sense.

Code: Select all

AddMultipleElements(List(), 1,3,-5,8,10)
ForEach List()
Next
But a special variant for lists with structures are required.
Maybe {1, "bla", 0} or something like that.

Bernd

Re: FOR-Loop with list numbers

Posted: Tue Oct 24, 2017 9:39 pm
by mk-soft

Code: Select all

Macro dq
  "
EndMacro

Macro AddElementValues(List, v1, v2=, v3=, v4=, v5=, v6=, v7=, v8=)
  LastElement(List)
  AddElement(List)
  List = v1
  CompilerIf Bool(dq#v2#dq <> "") : AddElement(List) : List = v2 : CompilerEndIf
  CompilerIf Bool(dq#v3#dq <> "") : AddElement(List) : List = v3 : CompilerEndIf
  CompilerIf Bool(dq#v4#dq <> "") : AddElement(List) : List = v4 : CompilerEndIf
  CompilerIf Bool(dq#v5#dq <> "") : AddElement(List) : List = v5 : CompilerEndIf
  CompilerIf Bool(dq#v6#dq <> "") : AddElement(List) : List = v6 : CompilerEndIf
  CompilerIf Bool(dq#v7#dq <> "") : AddElement(List) : List = v7 : CompilerEndIf
  CompilerIf Bool(dq#v8#dq <> "") : AddElement(List) : List = v8 : CompilerEndIf
EndMacro

NewList MyList()

AddElementValues(MyList(), 1,3,-5,8,10)
ForEach MyList()
  Debug MyList()
Next
:wink:

Re: FOR-Loop with list numbers

Posted: Wed Oct 25, 2017 10:32 pm
by Mijikai
+1

Re: FOR-Loop with list numbers

Posted: Thu Oct 26, 2017 9:34 am
by Dude
infratec wrote:why not use a List() and ForEach ?
I see no real reason for the 'other' syntax.
Data can be used, too:

Code: Select all

Repeat
  Read n
  Debug n
Until n=999

DataSection
  Data.l 1,2,7,8,-5,100,999
EndDataSection

Re: FOR-Loop with list numbers

Posted: Thu Oct 26, 2017 4:25 pm
by kenmo

Code: Select all

Macro dq
  "
EndMacro

Macro AddElementValues(List, v1, v2=, v3=, v4=, v5=, v6=, v7=, v8=)
  LastElement(List)
  AddElement(List)
  List = v1
  CompilerIf Bool(dq#v2#dq <> "") : AddElement(List) : List = v2 : CompilerEndIf
  CompilerIf Bool(dq#v3#dq <> "") : AddElement(List) : List = v3 : CompilerEndIf
  CompilerIf Bool(dq#v4#dq <> "") : AddElement(List) : List = v4 : CompilerEndIf
  CompilerIf Bool(dq#v5#dq <> "") : AddElement(List) : List = v5 : CompilerEndIf
  CompilerIf Bool(dq#v6#dq <> "") : AddElement(List) : List = v6 : CompilerEndIf
  CompilerIf Bool(dq#v7#dq <> "") : AddElement(List) : List = v7 : CompilerEndIf
  CompilerIf Bool(dq#v8#dq <> "") : AddElement(List) : List = v8 : CompilerEndIf
EndMacro

NewList MyList()

AddElementValues(MyList(), 1,3,-5,8,10)
ForEach MyList()
  Debug MyList()
Next
:wink:
Oh nice!
Did Macro behavior change sometime? I thought you couldn't do this (I thought CompilerIfs were processed BEFORE Macros were expanded out),
but this method could be used in many ways!

Re: FOR-Loop with list numbers

Posted: Thu Oct 26, 2017 5:10 pm
by Marc56us
Another simple way using Select:

Code: Select all

For i = -5 To 100
     Select i
          Case 1,2,7,8,-5,100
               Debug i
     EndSelect
Next
:wink:

(But order not followed :| )

Code: Select all

-5
1
2
7
8
100
But can do intervals :wink:

Code: Select all

Case 1 To 5, 7, 8, -5, 100

Re: FOR-Loop with list numbers

Posted: Thu Oct 26, 2017 5:43 pm
by Little John
kenmo wrote:Did Macro behavior change sometime? I thought you couldn't do this (I thought CompilerIfs were processed BEFORE Macros were expanded out)
This is the behaviour of PureBasic macros at least since July 2010, when freak explained it on the German forum. ;-)

The code inside of a macro definition is not executed. The compiler only reads it! So if a macro is defined but never used in the code, then it will have no effect at all.

In the place where the macro is used, there it will be expanded, i.e. the macro name will be replaced with the macro contents, and only in that place the contents of the macro will take effect.

@mk-soft:
I think after If or CompilerIf, Bool() is not necessary.

Re: FOR-Loop with list numbers

Posted: Thu Oct 26, 2017 8:41 pm
by kenmo
Strange... in some ways, CompilerIf must be evaluated before Macros, or else code like this would not compile (multiple-definition macro)

Code: Select all

CompilerIf (#PB_Compiler_OS = #PB_OS_Windows)

  Macro SelectGadget(Gadget)
    SendMessage_(GadgetID(Gadget), #EM_SETSEL, 0, -1)
    SetActiveGadget(Gadget)
  EndMacro
  
CompilerElse

  Macro SelectGadget(Gadget)
    SetActiveGadget(Gadget)
  EndMacro
  
CompilerEndIf

Re: FOR-Loop with list numbers

Posted: Fri Oct 27, 2017 12:25 am
by Little John
kenmo wrote:Strange... in some ways, CompilerIf must be evaluated before Macros, or else code like this would not compile (multiple-definition macro)

Code: Select all

CompilerIf (#PB_Compiler_OS = #PB_OS_Windows)

  Macro SelectGadget(Gadget)
    SendMessage_(GadgetID(Gadget), #EM_SETSEL, 0, -1)
    SetActiveGadget(Gadget)
  EndMacro
  
CompilerElse

  Macro SelectGadget(Gadget)
    SetActiveGadget(Gadget)
  EndMacro
  
CompilerEndIf
I was talking only about code (including CompilerIf etc.) inside of a macro definition, as in mk-soft's example.

Or as in this example:

Code: Select all

Macro SelectGadget(Gadget)
   CompilerIf #PB_Compiler_OS = #PB_OS_Windows
      
      SendMessage_(GadgetID(Gadget), #EM_SETSEL, 0, -1)
      SetActiveGadget(Gadget)
      
   CompilerElse
      
      SetActiveGadget(Gadget)
      
   CompilerEndIf
EndMacro
Your above example is different :!:
In your example, the compiler directives are outside of any macro definition. Since they are part of the "normal" code, they will always be executed.

So whatever the compiler encounters first will take precedence: either the begin of a macro definition or a compiler directive.