Jump to content

How do I _ArrayToString($a2DArray)


Recommended Posts

This was wishful thinking - I took the example from the help manual but could not figure it out.

I need the 2d array string combined for a combobox list.

Please help.

#include <Array.au3>

Local $avArray[2][5] = [["JPM", "Holger", "Jon", "Larry", "Jeremy"], ["Valik", "Cyberslug", "Nutster", "JdeB", "Tylo"]]
;_ArrayDisplay($avArray, "$avArray as a 2D array")
_ArrayDisplay($avArray, "$avArray as a 2D array, transposed", -1, 1)

MsgBox(0, "_ArrayToString()", _ArrayToString($avArray));????????????
Link to comment
Share on other sites

  • Moderators

The _ArrayToString is only for single dimensions.

You'd have to loop through your 2d array and put the info in the order you wanted with the delim you wanted.

pseudo

Global $s_delim = "|"
Global $s_out = ""
For $i = 0 To UBound($avArray, 1) - 1 ; Start with dimension one
    For $j = 0 To UBound($avArray, 2) - 1
        $s_out &= $avArray[$i][$j] & $s_delim
    Next
Next
$s_out = StringTrimRight($s_out, StringLen($s_delim))
ConsoleWrite($s_out & @CRLF)
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Thanks Smoke_N. Much appreciated. So when you say put into order I want – do you mean split, search and concatenate after the loop or must I first rearrange the loop to give me the preferred order ie. reading array left to right: JPM|Valik|Holger|Cyberslug|Jon|Nutster|Larry|JdeB|Jeremy|Tylo

Link to comment
Share on other sites

  • Moderators

Thanks Smoke_N. Much appreciated. So when you say put into order I want – do you mean split, search and concatenate after the loop or must I first rearrange the loop to give me the preferred order ie. reading array left to right: JPM|Valik|Holger|Cyberslug|Jon|Nutster|Larry|JdeB|Jeremy|Tylo

Yes, you'd need to concatenate as I showed in the example. Without looking at _ArrayToString(), I'm sure this is what it does.

Left to Right is done in the loops I provided the pseudo code for.

If you need to go backwards (right to left) on the first dimension only you'd do something like:

For $i = UBound($avArray, 1) - 1 To 0 Step - 1
    For $j = 0 To UBound($avArray, 2) - 1

If you need to go backwards (right to left) on the 2nd dimension only:

For $i = 0 To UBound($avArray, 1) - 1
    For $j = UBound($avArray, 2) - 1 To 0 Step -1

Or if you wanted the whole thing backwards ( right to left ):

For $i = UBound($avArray, 1) - 1 To 0 Step - 1
    For $j = UBound($avArray, 2) - 1 To 0 Step -1

The options are really up to you.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

Thanks, I think I understood everything except:

Left to Right is done in the loops I provided the pseudo code for.

I could not figure out how to do that so I did it a long way by added another loop to list it in Left to Right sequence.

#include <Array.au3>

Local $avArray[2][5] = [["JPM", "Holger", "Jon", "Larry", "Jeremy"],["Valik", "Cyberslug", "Nutster", "JdeB", "Tylo"]]
_ArrayDisplay($avArray, "$avArray as a 2D array, transposed", -1, 1)

Global $s_delim = "|"
Global $s_out = ""

For $i = 0 To UBound($avArray, 1) - 1
    For $j = 0 To UBound($avArray, 2) - 1
        $s_out &= $avArray[$i][$j] & $s_delim
    Next
Next

$s_out = StringTrimRight($s_out, StringLen($s_delim))
$aNewArray = StringSplit($s_out, '|', 1)

Local $s_out2 = ""
For $i = 1 to (UBound($aNewArray, 1) - 1) / 2
    $s_out = $aNewArray[$i] & "|" & $aNewArray[$i + (UBound($aNewArray, 1) - 1) / 2] & $s_delim
    $s_out2 = $s_out2 & $s_out
Next

ConsoleWrite($s_out2 & @CRLF)

Can you now please tell me how to do this in your pseudo code nested loops?

Edited by 1905russell
Link to comment
Share on other sites

  • Moderators

You mean this?

Local $avArray[2][5] = [["JPM", "Holger", "Jon", "Larry", "Jeremy"],["Valik", "Cyberslug", "Nutster", "JdeB", "Tylo"]];,["Grape", "Orange", "Bannana", "Peach", "Apple"]]

Global $s_delim = "|"
Global $s_out = ""
Global $i_ub1 = UBound($avArray, 1) - 1
Global $i_ub2 = UBound($avArray, 2) - 1

For $i = 0 To $i_ub2
    For $j = 0 To $i_ub1
        $s_out &= $avArray[$j][$i] & $s_delim
    Next
Next

$s_out = StringTrimRight($s_out, StringLen($s_delim))
ConsoleWrite($s_out & @CRLF)

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

You mean this?

Local $avArray[2][5] = [["JPM", "Holger", "Jon", "Larry", "Jeremy"],["Valik", "Cyberslug", "Nutster", "JdeB", "Tylo"]];,["Grape", "Orange", "Bannana", "Peach", "Apple"]]

Global $s_delim = "|"
Global $s_out = ""
Global $i_ub1 = UBound($avArray, 1) - 1
Global $i_ub2 = UBound($avArray, 2) - 1

For $i = 0 To $i_ub2
    For $j = 0 To $i_ub1
        $s_out &= $avArray[$j][$i] & $s_delim
    Next
Next

$s_out = StringTrimRight($s_out, StringLen($s_delim))
ConsoleWrite($s_out & @CRLF)

Exactly what I mean - great technique (as always)– won't forget - fabulous Smoke_N - Thanks again.
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...