SRT to Join array elements

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
hagchr
Automation Wizard
Posts: 327
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

SRT to Join array elements

Post by hagchr » Thu Jun 19, 2014 11:09 am

I frequently need to combine array results (eg from RegEx) back into a string with some delimiter between the items. There is a Join function in VBS but I have not been able to figure out how (if at all possible) to make it work with <array> variables. Instead I ended up solving it with an SRT, which is called through:

GoSub>Join,"Array Name",delimiter,Ending delimiter 0/1,"Result Variable"

Both the array and the result variable names need to be in quotation marks.

(Not sure if this is an overkill to solve it. Please share if there is a better way to solve it that covers all arrays).

(Technical. When trying different versions of it I noted that, if I change ArrayCount>Join_Arr_1,Join_Arr_Count to ArrayCount>%Join_Arr_1%,Join_Arr_Count then MS is crashing for me).


Code: Select all

Let>Test=Here is a new list with numbers 83 465 53 24 4533 19 22 5

Let>delimiter=,

RegEx>\d+,Test,0,M,NM,0
Gosub>Join,"M",delimiter,0,"result"
MessageModal>result

//SRT Join,"Array Name",delimiter,ending delimiter 0/1,"Result Variable Name"
SRT>Join
    //Get the Array name w/o "" into Join_Arr_1
    RegEx>(?<=^").+(?="$),%Join_Var_1%,0,Join_Arr,Join_N,0
    ArrayCount>Join_Arr_1,Join_Arr_Count

    Let>Join_delimiter=%Join_Var_2%

    Let>Join_Res=
    Let>Join_ct=0
    While>Join_ct<Join_Arr_Count
        Let>Join_ct=%Join_ct%+1
        Let>Join_tmp=%Join_Arr_1%_%Join_ct%
        Let>Join_Res=%Join_Res%%Join_tmp%%Join_delimiter%
    EndWhile
    If>Join_Var_3=0
        RegEx>%Join_delimiter%(?=$),Join_Res,0,Matches,NumMatches,1,,Join_Res
    Endif

    //Get the Result Variable name w/o "" into Join_ResOut_1
    RegEx>(?<=^").+(?="$),%Join_var_4%,0,Join_ResOut,N,0
        
    Let>%Join_ResOut_1%=%Join_Res%
END>Join

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts