Output Each Element in an Array Onto Its Own Line in 1 MessageBox Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
theholyscripture
Posts: 62
Joined: 26 Apr 2019, 17:11

Output Each Element in an Array Onto Its Own Line in 1 MessageBox

21 Jan 2020, 13:52

Say I have the following array:

Code: Select all

obj:= ["thing1", "thing2", "thing3", "thing4"]
How would I go about creating a single MsgBox that outputs:
The Objects in the array are:

thing1
thing2
thing3
thing4
Thank you.
User avatar
JoeWinograd
Posts: 1411
Joined: 10 Feb 2014, 20:00

Re: Output Each Element in an Array Onto Its Own Line in 1 MessageBox  Topic is solved

21 Jan 2020, 14:04

Here you go:

Code: Select all

obj:=["thing1","thing2","thing3","thing4"]
AllElements:=""
NumElements:=ObjLength(obj)
Loop,%NumElements%
  AllElements:=AllElements . obj[A_Index] . "`n"
MsgBox,4160,All Elements,The Objects in the array are:`n`n%AllElements%
Regards, Joe
User avatar
Chunjee
Posts: 383
Joined: 18 Apr 2014, 19:05
GitHub: Chunjee

Re: Output Each Element in an Array Onto Its Own Line in 1 MessageBox

21 Jan 2020, 14:33

Example requires biga.ahk

Code: Select all

A := new biga()
obj:=["thing1","thing2","thing3","thing4"]

msgbox, % "The Objects in the array are:`n`n" A.join(obj, "`n")
theholyscripture
Posts: 62
Joined: 26 Apr 2019, 17:11

Re: Output Each Element in an Array Onto Its Own Line in 1 MessageBox

21 Jan 2020, 14:58

JoeWinograd wrote:
21 Jan 2020, 14:04
Here you go:

Code: Select all

obj:=["thing1","thing2","thing3","thing4"]
AllElements:=""
NumElements:=ObjLength(obj)
Loop,%NumElements%
  AllElements:=AllElements . obj[A_Index] . "`n"
MsgBox,4160,All Elements,The Objects in the array are:`n`n%AllElements%
Regards, Joe
Thanks Joe, that did the trick for me. I believe I'm able to understand what's going on in your code as well.


Chunjee wrote:
21 Jan 2020, 14:33
Example requires biga.ahk

Code: Select all

A := new biga()
obj:=["thing1","thing2","thing3","thing4"]

msgbox, % "The Objects in the array are:`n`n" A.join(obj, "`n")
Thanks Chunjee, that looks like it saves a bit of extra code.
User avatar
JoeWinograd
Posts: 1411
Joined: 10 Feb 2014, 20:00

Re: Output Each Element in an Array Onto Its Own Line in 1 MessageBox

21 Jan 2020, 15:14

> that did the trick for me

Happy to hear that!

> I believe I'm able to understand what's going on in your code as well.

Even happier to hear that! It's an explicit goal of mine, as I much prefer clarity over brevity in my code. I use long variable names, avoid language shortcuts (such as +=, .=, implicit concatenation, assumed variable initialization, etc.), and, in general, try to make my code easy to understand and modify. Regards, Joe
FanaticGuru
Posts: 1399
Joined: 30 Sep 2013, 22:25

Re: Output Each Element in an Array Onto Its Own Line in 1 MessageBox

21 Jan 2020, 15:53

Here is a really great function from the String Things library.

Code: Select all

/*
; inspired by Lexikos ExploreObj()
PrintArr
   Prints the layout of an array (in text form) so that you can easily
   see the layout of it.

   array = The input array. Don't add the []'s.
   depth = How deep should we go if there are arrays within arrays?
   * indentLevel should not be touched as it's hard-coded in! DO NOT TOUCH.

example: st_printArr([["aaa","bbb","ccc"],[111,222,333]])
output:
[1]
    [1] ==> aaa
    [2] ==> bbb
    [3] ==> ccc

[2]
    [1] ==> 111
    [2] ==> 222
    [3] ==> 333
*/
st_printArr(array, depth=5, indentLevel="")
{
   for k,v in Array
   {
      list.= indentLevel "[" k "]"
      if (IsObject(v) && depth>1)
         list.="`n" st_printArr(v, depth-1, indentLevel . "    ")
      Else
         list.=" => " v
      list.="`n"
   }
   return rtrim(list)
}

Then you can do this:

Code: Select all

obj:=["thing1","thing2","thing3","thing4"]
MsgBox % st_printArr(obj)
This will look fairly simple but this function will get you a nice graphic display of multi-dimensional associative arrays that lets you better see what is going on in complex array structures.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts

AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon

[Function] Timer - Create and Manage Timers

Return to “Ask For Help”

Who is online

Users browsing this forum: BreezeB, JoeWinograd, poetbox, TAC109 and 318 guests