Community discussions

MikroTik App
 
zivtal
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 57
Joined: Sun Feb 05, 2017 6:22 pm

Load functions from another script

Wed Aug 01, 2018 4:57 pm

Hi, I have a lot's of scripts on-up / on-down / on-login / on-logout and etc,,...

I some of them I'm using same functions that I made, Can I put the function in script and call it from those scripts (on-up/on-down and etc...) ?

This is my useful functions, It's get input and return output... for example:
:local str "NAME1=TEST1;NAME2=TEST2;NAME3=TEST3"
:set str [$setKeyValue string=[:tostr $str] name="NAME2" value="CHANGED"]
The str output will be "NAME1=TEST1;NAME2=CHANGED;NAME3=TEST3"

:local getKeyValue do={
	:if ([:type [:find $string $name]]!="nil") do={
		:local cut [:pick $string ([:find $string $name]+[:len $name]+1) [:len $string]]
		:if ([:len [:find $cut ";"]]>0) do={
			:return [:pick $cut 0 [:find $cut ";"]]
		} else={
			:return [:pick $cut 0 [:len $cut]]
		}
	} else={
		:return ""
	}
}

:local setKeyValue do={
	:if ([:typeof [:find $string $name]]!="nil") do={
		:local current
		:local cut [:pick $string ([:find $string $name]) [:len $string]]
		:if ([:len [:find $cut ";"]]>0) do={
			:set current [:pick $cut 0 [:find $cut ";"]]
		} else={
			:set current [:pick $cut 0 [:len $cut]]
		}
		:return ([:pick $string 0 [:find $string $current]]."$name=$value".[:pick $string ([:find $string $current]+[:len $current]) [:len $string]])
	} else={
		:return ("$name=$value;".$string)
	}
}
 
User avatar
dasiu
Trainer
Trainer
Posts: 231
Joined: Fri Jan 30, 2009 11:41 am
Location: Reading, UK
Contact:

Re: Load functions from another script

Thu Aug 02, 2018 1:31 pm

Yes, you can :).
1. Make the getKeyValue and setKeyValue functions :global instead of :local
2. Run the one script defining all those global functions on startup
3. In the beginning of your script using the functions put:
:global getKeyValue;
:global setKeyValue;

So the globals are in the memory all the time, in each script you just "declare" you're using the global value, so script knows where to look that variable/function name.
 
zivtal
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 57
Joined: Sun Feb 05, 2017 6:22 pm

Re: Load functions from another script

Sun Aug 05, 2018 11:11 am

Yes, you can :).
1. Make the getKeyValue and setKeyValue functions :global instead of :local
2. Run the one script defining all those global functions on startup
3. In the beginning of your script using the functions put:
:global getKeyValue;
:global setKeyValue;

So the globals are in the memory all the time, in each script you just "declare" you're using the global value, so script knows where to look that variable/function name.
I know that way, I prefer put it in another script and call (load) it from each script that I need it... If there is way to do it...
 
User avatar
dasiu
Trainer
Trainer
Posts: 231
Joined: Fri Jan 30, 2009 11:41 am
Location: Reading, UK
Contact:

Re: Load functions from another script

Mon Aug 06, 2018 11:16 am

You prefer to "load" it... where? Isn't "loading" to /system script environment, to the list of global variables/functions, enough?
That's what I use - I have an .rsc file with "functions" I just run - and it loads the functions to globals. And other file to remove them. And in the main script I just /import the file, use the functions, and then "clear" them.

Who is online

Users browsing this forum: No registered users and 39 guests