Convert array to string Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
john_c
Posts: 175
Joined: 05 May 2017, 13:19

Convert array to string

14 Jul 2018, 03:30

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.
Last edited by john_c on 14 Jul 2018, 04:24, edited 1 time in total.
just me
Posts: 5577
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Convert array to string

14 Jul 2018, 04:02

Code: Select all

Str := ""
For Index, Value In Array
   Str .= Value . "|"
Str := RTrim(Str, "|") ; remove the last pipe (|)
john_c
Posts: 175
Joined: 05 May 2017, 13:19

Re: Convert array to string

14 Jul 2018, 04:21

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?
just me
Posts: 5577
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Convert array to string  Topic is solved

14 Jul 2018, 04:44

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%
?
john_c
Posts: 175
Joined: 05 May 2017, 13:19

Re: Convert array to string

14 Jul 2018, 05:50

?
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
john_c
Posts: 175
Joined: 05 May 2017, 13:19

Re: Convert array to string

14 Jul 2018, 06:20

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.
just me
Posts: 5577
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Convert array to string

14 Jul 2018, 07:07

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.
john_c
Posts: 175
Joined: 05 May 2017, 13:19

Re: Convert array to string

14 Jul 2018, 08:42

...
Understand, thanks!

Return to “Ask For Help”

Who is online

Users browsing this forum: loek3000, MannyKSoSo, MikeyG12, Scr1pter and 34 guests