Page 1 of 1

Convert array to string

Posted: 14 Jul 2018, 03:30
by john_c
Is there way to convert array into string?

Test.ini:

Code: Select all

[Red]
foo1=bar1
foo2=bar2

[Green]
foo3=bar3
foo4=bar4
Test.ahk:

Code: Select all

Array := []
Loop
{
    FileReadLine, Line, Test.ini, %A_Index%
    if ErrorLevel
        Break
    Matched := RegExMatch(Line, "i)^\[.+\]")
    if Matched = 1
        Array.Push(A_LoopReadLine)
}

; And next, the goal is to convert this array into "Red|Green" string (or "RedGreen" without vertical bar).
; But I don't understand how to achieve it.
; I also tried to use pseudo-array but it doesn't look simpler for me.

Re: Convert array to string

Posted: 14 Jul 2018, 04:02
by just me

Code: Select all

Str := ""
For Index, Value In Array
   Str .= Value . "|"
Str := RTrim(Str, "|") ; remove the last pipe (|)

Re: Convert array to string

Posted: 14 Jul 2018, 04:21
by john_c
just me wrote:

Code: Select all

Str := ""
For Index, Value In Array
   Str .= Value . "|"
Str := RTrim(Str, "|") ; remove the last pipe (|)
Thank you, but I don't sure it works for me. I tried to add "MsgBox, %Str%" at the bottom and it doesn't show me anything. AHK version 1.1.27.07. Does it work for you?

Re: Convert array to string  Topic is solved

Posted: 14 Jul 2018, 04:44
by just me

Code: Select all

#NoEnv
Array := []
Array.Push("Red")
Array.Push("Green")

Str := ""
For Index, Value In Array
   Str .= "|" . Value
Str := LTrim(Str, "|") ; Remove leading pipes (|)
MsgBox, %Str%
?

Re: Convert array to string

Posted: 14 Jul 2018, 05:50
by john_c
just me wrote:?
Yes, it works in your example, thanks. Now I understand that there is an error in my initial script and will try to find it

Re: Convert array to string

Posted: 14 Jul 2018, 06:20
by john_c
just me wrote:

Code: Select all

#NoEnv
Array := []
Array.Push("Red")
Array.Push("Green")
It's a bit offtopic here, but could you please elaborate the difference between "Array.Push" and "Array.Insert"? They seems to work identically in provided example.

Re: Convert array to string

Posted: 14 Jul 2018, 07:07
by just me
[b]Insert[/b] wrote:Deprecated: Insert is not recommended for use in new scripts. Use InsertAt, Push, ObjRawSet or a simple assignment instead.
BTW: A_LoopReadLine is only defined within a file-reading loop.

Re: Convert array to string

Posted: 14 Jul 2018, 08:42
by john_c
just me wrote:...
Understand, thanks!