The MASM Forum

General => The Workshop => Topic started by: jj2007 on May 13, 2016, 01:04:30 AM

Title: For each x$ in My$()
Post by: jj2007 on May 13, 2016, 01:04:30 AM
I've always wondered what VB's for each var in collection syntax was good for. Now here is a test:
include \masm32\MasmBasic\MasmBasic.inc
  Init
  Dim My$()
  Dim MyDw() As DWORD ; WORD + BYTE are OK, too
  Dim MyQw() As QWORD
  Dim MyByte() As BYTE
  Dim MyR4() As REAL4
  Dim MyR8() As REAL8
  Dim MyR10() As REAL10
  Dim rc() As RECT

  For_ ecx=0 To 2 ; we create 3 elements in each array
Let My$(ecx)=Str$("String %i", ecx) ; fill the string array
mov rc(ecx, left), ecx ; fill the RECT array
push ecx
fild stack
fld1
fadd
fmul FP8(1111222233330000000.0)
fistp MyQw(ecx) ; fill the QWORD array
pop edx
add rc(ecx, left), 100
mov rc(ecx, right), ecx
add rc(ecx, right), 200
mov MyDw(ecx), ecx ; fill the DWORD array
add MyDw(ecx), 100
mov MyByte(ecx), cl
add MyByte(ecx), 100 ; fill the BYTE array
fldpi
fild MyDw(ecx)
fadd
fst MyR4(ecx) ; load real4
fst MyR8(ecx) ; and real8 arrays
fstp MyR10(ecx) ; even Real10 is OK
  Next

  PrintLine "------------- Qword array:" ; time to print the elements
  For_ each q in MyQw()
PrintLine "[", Str$(q), "]"
  Next
  PrintLine "------------- String x$:"
  For_ each x$ in My$()
PrintLine "[", x$, "]"
  Next
  PrintLine "------------- String reg32:"
  For_ each eax in My$()
PrintLine "[", eax, "]" ; jjj
  Next
  PrintLine "------------- Rect:"
  For_ each rcx in rc()
Print Str$(ForNextCounter), Tb$
PrintLine Str$("[%i] left", rcx.left), Str$(", [%i] right", rcx.right)
  Next
  PrintLine "------------- Real4:"
  For_ each varReal in MyR4()
PrintLine Str$("[%Hf]", varReal)
  Next
  PrintLine "------------- Real8:"
  For_ each varReal in MyR8()
PrintLine Str$("[%Hf]", varReal)
  Next
  PrintLine "------------- Real10:"
  For_ each varReal in MyR10()
PrintLine Str$("[%Jf]", varReal)
  Next
  Print Str$("PI=%Jf\n", PI)
  PrintLine "------------- Dword:"
  For_ each varDw in MyDw()
PrintLine Str$("[%i]", varDw), Str$("\t at ct=%i", ForNextCounter)
  Next
  PrintLine "------------- Dword (repeated):"
  For_ each varDw in MyDw()
PrintLine Str$("[%i]", varDw), Str$("\t at ct=%i", ForNextCounter)
  Next
  PrintLine "------------- Byte:"
  For_ each varDw in MyByte()
PrintLine Str$("[%i]", varDw), Str$(" - repeated: [%i]", varDw)
  Next
  Inkey "-- done -----------"
EndOfCode


Output:------------- Qword array:
[1111222233330000000]
[2222444466660000000]
[3333666699990000000]
------------- String x$:
[String 0]
[String 1]
[String 2]
------------- String reg32:
[String 0]
[String 1]
[String 2]
------------- Rect:
0       [100] left, [200] right
1       [101] left, [201] right
2       [102] left, [202] right
------------- Real4:
[103.14159393310547]
[104.14159393310547]
[105.14159393310547]
------------- Real8:
[103.14159265358980]
[104.14159265358980]
[105.14159265358980]
------------- Real10:
[103.1415926535897932]
[104.1415926535897932]
[105.1415926535897932]
PI=3.141592653589793238
------------- Dword:
[100]    at ct=0
[101]    at ct=1
[102]    at ct=2
------------- Dword (repeated):
[100]    at ct=0
[101]    at ct=1
[102]    at ct=2
------------- Byte:
[100] - repeated: [100]
[101] - repeated: [101]
[102] - repeated: [102]
-- done -----------


Beta attached - use at your own risk, Final version uploaded on 14 May 2016 (http://masm32.com/board/index.php?topic=94.0). Give me feedback especially on the syntax 8)
Title: Re: For each x$ in My$()
Post by: jj2007 on May 13, 2016, 01:49:26 AM
Two demos attached. This one shows the positions, dimensions and captions of all top level windows.

include \masm32\MasmBasic\MasmBasic.inc
  ct dd ?            ; global string counter
  Init

  Dim caption$()
  Dim topwinrect() As RECT
  MyEnum PROTO :DWORD, :DWORD
  invoke EnumWindows, MyEnum, 0
  Print cfm$("#\tx\ty\tw\th\tcaption")
  For_ each rcx in topwinrect()
      Print Str$("\n%i\t", ForNextCounter), Str$(rcx.left), Tb$, Str$(rcx.top), Tb$, Str$(rcx.right), Tb$, Str$(rcx.bottom), Tb$, Left$(caption$(ForNextCounter), 40)
  Next_

  Exit

MyEnum proc uses esi hwnd, dummyarg
  mov esi, Win$(hwnd)
  .if Len(esi)
      .if StringsDiffer(esi, "MSCTFIME UI")            ; exclude some
            .if StringsDiffer(esi, "Default IME")            ; boring little windows
                  Let caption$(ct)=esi
                  invoke GetWindowRect, hwnd, VarPtr(topwinrect(ct))      ; store the dimensions
                  inc ct
            .endif
      .endif
  .endif
  or eax, -1      ; continue
  ret
MyEnum endp

EndOfCode
Title: Re: For each x$ in My$()
Post by: Siekmanski on May 13, 2016, 05:15:32 AM
------------- Qword array:
[1111222233330000000]
[2222444466660000000]
[3333666699990000000]
------------- String x$:
[String 0]
[String 1]
[String 2]
------------- String reg32:
[String 0]
[String 1]
[String 2]
------------- Rect:
0       [100] left, [200] right
1       [101] left, [201] right
2       [102] left, [202] right
------------- Real4:
[103.14159393310547]
[104.14159393310547]
[105.14159393310547]
------------- Real8:
[103.14159265358980]
[104.14159265358980]
[105.14159265358980]
------------- Real10:
[103.1415926535897932]
[104.1415926535897932]
[105.1415926535897932]
PI=3.141592653589793238
------------- Dword:
[100]    at ct=0
[101]    at ct=1
[102]    at ct=2
------------- Dword (repeated):
[100]    at ct=0
[101]    at ct=1
[102]    at ct=2
------------- Byte:
[100] - repeated: [100]
[101] - repeated: [101]
[102] - repeated: [102]
-- done -----------


Title: Re: For each x$ in My$()
Post by: jj2007 on May 20, 2016, 10:38:18 PM
Thanks, Marinus :icon14:

I am tempted to allow also
For_ each x$()
  Print "The element of the array: [", each, "]"
Next_

For_ each R8()
  Print Str$("The element of the array: [%f]", each)
Next_


It would be actually non-ambiguous because
- no to
- no in
- last arg must be an array identifier

But it looks a bit ugly ::)

Java solves it like this: (http://docs.oracle.com/javase/1.5.0/docs/guide/language/foreach.html)
    for (TimerTask t : c)
        t.cancel();


QuoteWhen you see the colon (:) read it as "in." The loop above reads as "for each TimerTask t in c."

In short: If you don't understand The Oracle, hire an interpreter 8)