FOR-Loop with list numbers

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Lebostein
Addict
Addict
Posts: 807
Joined: Fri Jun 11, 2004 7:07 am

FOR-Loop with list numbers

Post 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
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: FOR-Loop with list numbers

Post by RSBasic »

+1
Image
Image
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: FOR-Loop with list numbers

Post 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
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: FOR-Loop with list numbers

Post 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:
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: FOR-Loop with list numbers

Post by Mijikai »

+1
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: FOR-Loop with list numbers

Post 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
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: FOR-Loop with list numbers

Post 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!
Marc56us
Addict
Addict
Posts: 1477
Joined: Sat Feb 08, 2014 3:26 pm

Re: FOR-Loop with list numbers

Post 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
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: FOR-Loop with list numbers

Post 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.
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: FOR-Loop with list numbers

Post 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
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: FOR-Loop with list numbers

Post 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.
Post Reply