Jump to content

Check number of subscripts and display all?


Recommended Posts

Hi I've created a sample scripts that lists my variables in GUI a window. See below:

#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Global $var[7][1]

$var[0][0] = "Sunshine"
$var[1][0] = "Moonlight"
$var[2][0] = "Aurora"
$var[3][0] = "Eclipse"
$var[4][0] = "Dusk"
$var[5][0] = "Dawn"
$var[6][0] = "Sunrise"

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Item List", 521, 289, 192, 124)
$Label1 = GUICtrlCreateLabel("Here is the list of items", 16, 16, 109, 17)
$ItemList = GUICtrlCreateList("", 16, 40, 481, 227)

$i = 0
Do 
GUICtrlSetData(-1, $var[$i][0])
$i = $i +1
Until $i = 7


GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
$nMsg = GUIGetMsg()
Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit

EndSwitch
WEnd

This will list off all items (even arranges them alphabetically.. not sure how though) because I know the exact amount of subscripts that is fed into the DO LOOP

Until $i = 7

Is there a way for it to check the number of subscripts within the array variable and display it without me having to tell it the number of subscripts? Posted Image

Any help would be greatly appreciated! Posted Image

Link to comment
Share on other sites

Short answer: Ubound()

long answer:

Local $array[4] = ["element0","element1","element2","element3"]

ConsoleWrite("Example 1:" & @CRLF)
For $i = 0 To UBound($array) -1 ;Ubound returns the size of the array
    ConsoleWrite($array[$i] & @CRLF) ;$i will start at 0, then increase with 1 untill the value Ubound($array) -1 is reached.
Next

ConsoleWrite(@CRLF & "Example 2:" & @CRLF)
For $element In $array
    ConsoleWrite($element & @CRLF) ;element will be the next one on each run of the loop, then the loop will exit.
Next

Ofcoarse you could use Ubound with the Do...Untill, or a While...WEnd loop, but usually for loops are ideal.

Link to comment
Share on other sites

Thanks Tvern!! Posted Image

Here's my updated code!

#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Global $var[7][1]

$var[0][0] = "Sunshine"
$var[1][0] = "Moonlight"
$var[2][0] = "Aurora"
$var[3][0] = "Eclipse"
$var[4][0] = "Dusk"
$var[5][0] = "Dawn"
$var[6][0] = "Sunrise"

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Item List", 521, 289, 192, 124)
$Label1 = GUICtrlCreateLabel("Here is the list of items", 16, 16, 109, 17)
$ItemList = GUICtrlCreateList("", 16, 40, 481, 227)

For $i = 0 To UBound($var) -1
 GUICtrlSetData(-1, $var[$i][0])

Next

GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
 $nMsg = GUIGetMsg()
 Switch $nMsg
  Case $GUI_EVENT_CLOSE
   Exit

 EndSwitch
WEnd
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...