passing variable to subroutine

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
katalonija2
Junior Coder
Posts: 21
Joined: Thu Mar 10, 2011 6:49 pm

passing variable to subroutine

Post by katalonija2 » Tue Apr 05, 2011 11:29 am

I am interested in advices how to pass variables to subroutines.

As i understand i regulary pass values.
So gosub>subroute1,text,9
will pass "text" and "9" to srt>subroute1...

Aside from using global scope variables, is there way to pass variable to subr.
Or array....

For example I want to pass value of %list_5% variable (member of array).
What is sintax to do it if it is possible?
i can not pass it as gosub>srt1,%list_5% .. or ... gosub>srt1,list_5

Or gosub>srt1,{%list_5%}

So what is current best way of doing this?
I want to use subroutine as appendix for regular command set

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Apr 05, 2011 12:07 pm

I'm not sure why you say you can't pass the array variable. E.g. this works fine:

Code: Select all

GetFileList>%USERDOCUMENTS_DIR%\*.*,list,CRLF
Separate>list,CRLF,files

GoSub>test,files_5

SRT>test
  MessageModal>test_var_1
END>test
Have I misunderstood the question?
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

katalonija2
Junior Coder
Posts: 21
Joined: Thu Mar 10, 2011 6:49 pm

Post by katalonija2 » Tue Apr 05, 2011 1:42 pm

No, you didnot.
I looked in help,only value passing is mentioned.
So notation is

parameter of subroutine is checked if it is var or array then if it is var or array is passed, or at the end, value as text or number is passed.
Is that right?

gosub>srt1,param
>if param is var, then var is passed
>if param is array, then arrah is passed
>if nothing above, "param" is passed

katalonija2
Junior Coder
Posts: 21
Joined: Thu Mar 10, 2011 6:49 pm

array and array element

Post by katalonija2 » Tue Apr 12, 2011 8:08 am

Sorry,but i must inform others that passing array as variable do not work in MS.
You can assign in main program one array through another, but you can pass to subroutine only one element of array, not array by itself.

So if question was can ARRAY be passed to subroutine, answer is not, yes , becouse one element of array can be passed to subroutine.

I did not manage to pass array, but something strange happened.

Code: Select all

GetFileList>%SYS_DIR%\*.*,list,CRLF
Let>files="pera"
Separate>list,CRLF,files

will result in existance files="pera"
files_1,.......files_123.....
So in the same time exist array files and variable files
---------------------------------------
Other strange thing happened also.
When you call srt test with parameter that is array,

Code: Select all

ArrayCount>test_Var,test_count
will give number of array elements, but i can not access any of them.


------------------------------------------------
Here is some test code where you can see that test_count at breakpoint is equal as number of elements of array, but array is not passed.

Code: Select all

Let>k=1
Let>line=""
Let>tekst={""}
While>%line%<EOF>%SCRIPT_DIR%\..\DATA\proba.txt,k,line
  if>%line%=##EOF##,ende
  Separate>%line%,;,niz
   ArrayCount>niz,niz_count
   MessageModal>niz_count
   Let>tttt=0
    Repeat>tttt
    add>tttt,1
    Let>broj=niz_%tttt%
    ConCat>tekst,{%broj%}
    ConCat>tekst,{%CRLF%}
    Until>tttt=%niz_count%
    MessageModal>tekst

  GoSub>test,niz



 srt>test
 Let>tekst={""}
   ArrayCount>test_Var,test_count
   Let>nniz=test_Var
   ArrayCount>test_Var,test_count

**BREAKPOINT**
    Let>tt=0
    Repeat>tt
    add>tt,1
    Let>broj=test_Var_%tt%
    ConCat>tekst,{%broj%}
    ConCat>tekst,{%CRLF%}
    Until>tt=%niz_count%
    MessageModal>tekst

 END>test

EndWhile
Label>ende
short version

Code: Select all

Let>line="ksfefesgdsfs;sfesfefsefesf;sefsefgbfgsef;efesfeegefg;efesfsef"

   Separate>%line%,;,niz
   ArrayCount>niz,niz_count
   MessageModal>niz_count
 
  GoSub>test,niz


 srt>test
   ArrayCount>test_Var_1,test_count
**BREAKPOINT**
   Let>nniz=test_Var_1
   ArrayCount>nniz,nniz_count


 END>test

test_count has global scope, and that is problem to consider.
Last edited by katalonija2 on Tue Apr 12, 2011 10:47 am, edited 1 time in total.

katalonija2
Junior Coder
Posts: 21
Joined: Thu Mar 10, 2011 6:49 pm

Post by katalonija2 » Tue Apr 12, 2011 9:21 am

Can i assign array to another variable....Let>1array=2array?
Not by loop.

If this is not available, then it is not surprise that passing array to subr is not available...

wjfinniganjds
Junior Coder
Posts: 24
Joined: Tue Sep 15, 2009 3:20 pm

Arrays as a passed variable

Post by wjfinniganjds » Mon Apr 18, 2011 10:02 pm

Try this Syntax:

VARIABLE_%COUNTER%

I use this w/o the {} to import my variables. MS has done everything I can imagine so far.

You may want to read the variable into another variable first.

If you decide to send multiple items from your array to the subroutine I would try something like this:
Let>AddnlArrayItem=Variable_%LoopCounter%

Let>NewVariable=%NewVariable%,%AddnlArrayItem%

Then send to the subroutine using this syntax once your loop has added all the array items you wish sent.

gosub>srt1,%NewVariable%

Hope that helps.
I think I can automate that!

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