Jump to content
Pilot123

Arrays: Elements as variables

Recommended Posts

Pilot123

Hi,

 

I need help please.

I have code that returns an array, and then writes the results to a message box. There is three possible 2-d arrays.

 

1.)  2-d array with 2 rows

2.)  2-d array with 1 rows

3.)  2-d array with no rows

 

Is there a better way to get the results in the message box with the three given arrays as example? The format in the message boxes should stay the same. 

#include <Array.au3>

Local  $arr[2][2] = [["20:00","22:00"], ["14:00","18:00"]]    ;Array 1: 2-d array with two rows
;Local  $arr[1][2] = [["20:00","22:00"]]       ; Array 2: 2-d array with one rows
;Local  $arr[0][0] = [[]]         ; Array 3: 2-d array with 'no' data
;_ArrayDisplay($arr, "My Array1")

Local $iRows = UBound($arr, $UBOUND_ROWS)       ; Total number of rows found in the array.
;MsgBox($MB_SYSTEMMODAL, "$arr", "The array has " & $iRows & " rows")

If $iRows = 0 then
   MsgBox($MB_SYSTEMMODAL, "$arr", "Sorry!! No rows")
Else
   Endif

If $iRows = 1 then
   $var1 = $arr[0][0]
   $var2 = $arr[0][1]
   MsgBox($MB_SYSTEMMODAL, "$arr", "Has " & $iRows & " row:" & @CRLF & "Group 1: " & $var1 & " - " & $var2)
Else
   Endif

If $iRows = 2 then
   $var1 = $arr[0][0]
   $var2 = $arr[0][1]
   $var3 = $arr[1][0]
   $var4 = $arr[1][1]
   MsgBox($MB_SYSTEMMODAL, "$arr", "Has " & $iRows & " rows:" & @CRLF & "Group 1: " & $var1 & " - " & $var2 & @CRLF & "Group 2: " & $var3 & " - " & $var4 )
Else
   Endif

 

Edited by Pilot123

Share this post


Link to post
Share on other sites
SadBunny

Maybe something like this?

#include <Array.au3>

Local $arr[2][2] = [["20:00", "22:00"], ["14:00", "18:00"]] ;Array 1: 2-d array with two rows
;~ Local  $arr[1][2] = [["20:00","22:00"]]       ; Array 2: 2-d array with one rows
;~ Local  $arr[0][0] = [[]]         ; Array 3: 2-d array with 'no' data
;_ArrayDisplay($arr, "My Array1")

Local $iRows = UBound($arr, $UBOUND_ROWS) ; Total number of rows found in the array.
;MsgBox($MB_SYSTEMMODAL, "$arr", "The array has " & $iRows & " rows")

If $iRows = 0 Then
    MsgBox($MB_SYSTEMMODAL, "$arr", "Sorry!! No rows")
Else
    $s = "Has " & $iRows & " row" & ($iRows > 1 ? "s" : "") & ":" & @CRLF
    For $i = 0 to $iRows - 1
        $var1 = $arr[$i][0]
        $var2 = $arr[$i][1]
        $s &= "Group " & $i + 1 & ": " & $var1 & " - " & $var2 & @CRLF
    Next
    MsgBox(0, 0, $s)
EndIf

 


Roses are FF0000, violets are 0000FF... All my base are belong to you.

Share this post


Link to post
Share on other sites
iamtheky
#include<array.au3>

Local  $arr[2][2] = [["20:00","22:00"], ["14:00","18:00"]]    ;Array 1: 2-d array with two rows
;~ Local  $arr[1][2] = [["20:00","22:00"]]       ; Array 2: 2-d array with one rows
;~ Local  $arr[0][0] = [[]]         ; Array 3: 2-d array with 'no' data


msgbox(0, '' , ubound($arr) > 0 ? "There are " & ubound($arr) & " rows." & @CRLF & _ArraytoString($arr , " | ") : "no rows available")

 

and with vars

Local  $arr[2][2] = [["20:00","22:00"], ["14:00","18:00"]]    ;Array 1: 2-d array with two rows
;~ Local  $arr[1][2] = [["20:00","22:00"]]       ; Array 2: 2-d array with one rows
;~ Local  $arr[0][0] = [[]]         ; Array 3: 2-d array with 'no' data


for $i = 0 to ubound($arr) - 1
    assign("var" & $i , $arr[$i][0])
    assign("var" & $i & "c1" , $arr[$i][1])
Next


$TopLine = "There are " & ubound($arr) & " rows." & @LF
$data = ""
for $i = 0 to ubound($arr) - 1
$data &= "Group " & $i + 1 & ":  " & execute("$var" & $i) & " - " & execute("$var" & $i & "c1") & @LF
Next

msgbox(0, '' , $TopLine & $data)

 

Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
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

×