"Pass" Array to SRT

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

"Pass" Array to SRT

Post by hagchr » Tue Jun 17, 2014 4:50 pm

Hi, I frequently end up with results (eg from RegEx etc) in the form of <array> and have had some problem writing generic SRT or VBS where I can "pass" the array and resulting variable name as input and getting the result assigned to the variable name given in the call. If I understand it correctly you cannot pass the array itself so instead I pass the name of it and can then access the elements within the SRT, do the work, and send the result back. In order for it to work both the array and resulting variable need quotation marks in the GoSub call.

I put a (very) simple example below just to illustrate the solution that now works for me. Hopefully it can help someone. If you have a better way of solving it then please share.

Code: Select all

Let>Test=Here are some nice numbers 1 4 55 3 66 77 22 1234 22 44 2 22 44 222

//Calculate the sum of all numbers in the <array> "m1"
RegEx>\d+,Test,0,m1,NM,0
Gosub>calc,"m1","res1"
MessageModal>res1


//SRT to do generic calculation
SRT>calc
//Get array name and count elements
RegEx>(?<=^").+(?="$),calc_VAR_1,0,Match,NM,0
ArrayCount>Match_1,ct_max

Let>sum=0
Let>ct=0
While>ct<ct_max
    Let>ct=%ct%+1
    Let>sum=%sum%+%Match_1%_%ct%
EndWhile

//Get result variable name and assign value
RegEx>(?<=^").+(?="$),calc_VAR_2,0,MatchRes,NM,0
Let>%MatchRes_1%=sum
END>calc

mightycpa
Automation Wizard
Posts: 343
Joined: Mon Jan 12, 2004 4:07 pm
Location: Vienna, VA

Re: "Pass" Array to SRT - doesn't always work

Post by mightycpa » Thu Mar 26, 2015 4:12 am

Hi,

This is a nice idea, and I was hoping I could use it. I modified your code slightly to do a test, and it worked!

At first, anyway:

Code: Select all

Let>Test=Here are some nice numbers 1 4 AB 3 66 C7 22 1234 Hello 2 22 44 222
Separate>Test, ,m1
ArrayCount>m1,v_before
MDL>There are %v_before% elements in the array before the SRT
//Return the values
MDL>Yep: %Z1_7%
MDL>%Z1_7%
Gosub>calc,"m1","res1"

//SRT to do generic calculation
SRT>calc
//Get array name and count elements
ArrayCount>calc_VAR_1,v_after
Length>calc_VAR_1,v_len
MDL>count is %v_after% and len is %v_len%.  Calc var 1 is NOT an array
RegEx>\w\d,calc_VAR_1,0,Match,NM,0
// Notice that Match is the array, and Match_1 is the first element in the array
ArrayCount>Match_1,ct_element_1
MDL>After the RegEx, the array count for the first element of Match is : %ct_element_1%
//Unlike the original array, this fails to return the value
MDL>Nope: %Match_1%_7
//but this does return the element value
MDL>%Match_1%_7
END>calc
Don't ask me why I did this, but all I did was change the variable from m1 to MY_ARR..that didn't work. Neither did Z, or z, but when I tried z1, that worked. Next, I tried z2. That worked too. Then I tried Z1. That also worked. I tried Z11; failure. Finally, I tried 1Z and that failed. So in my limited testing, I found that you must name the array you wish to pass with a single letter and a single digit, in that order. Everything else fails.

Next, I took it a step further. Instead of RegEx or Separate, I did a DBQuery, returning my records into a recordset array m1. At this line:

Code: Select all

MDL>There are %v_before% elements in the array before the SRT
I found that there were zero elements in the recordset array. I'm not sure if it acts this way because the array is multidimensional, or if it is because there's something different about a recordset. Either way, that made the rest of the test fail.

So, you have to be very careful about the name of the array variable you pass, and you can't pass query recordsets. You also have to take care to isolate the values before you do much of anything with them.

Bummer.

It would be great to be able to pass an entire array, and an entire recordset.
"A facility for quotation covers the absence of original thought." - Lord Peter Wimsey

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

Re: "Pass" Array to SRT

Post by hagchr » Tue Mar 31, 2015 10:59 am

Hi, thanks for the reply. I modified the original code to your test string and changed the name to My_ARR and it still works fine. Key if you use this method is to keep the two regexes - one at the beginning of the SRT and one at the end which gives the unresolved array names, ie

Gosub>calc,"My_ARR","res1"

RegEx>(?<=^").+(?="$),calc_VAR_1,0,Match,NM,0
Will put the array name - My_ARR - into the variable Match_1 (unresolved)

RegEx>(?<=^").+(?="$),calc_VAR_2,0,MatchRes,NM,0
Let>%MatchRes_1%=sum
Will first put the variable name - res1 - into the variable MatchRes_1 (unresolved) then assign the value of sum to the variable res1.

Within the SRT, since the array name is in Match_1, you can easily get individual elements of the array, eg. element 3 can be obtained by %Match_1%_3

Hope this helps.

Code: Select all

Let>Test=Here are some nice numbers 1 4 AB 3 66 C7 22 1234 Hello 2 22 44 222

//Get all numbers into the array My_ARR
RegEx>\b\d+\b,Test,0,My_ARR,NM,0

//Calculate the sum of all numbers in the <array> "My_ARR"

Gosub>calc,"My_ARR","res1"
MessageModal>res1

//SRT to do generic calculation
SRT>calc
//Get array name and count elements
RegEx>(?<=^").+(?="$),calc_VAR_1,0,Match,NM,0
ArrayCount>Match_1,ct_max
Let>sum=0
Let>ct=0
While>ct<ct_max
    Let>ct=%ct%+1
    Let>sum=%sum%+%Match_1%_%ct%
EndWhile

//Get result variable name and assign value
RegEx>(?<=^").+(?="$),calc_VAR_2,0,MatchRes,NM,0
Let>%MatchRes_1%=sum
END>calc

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