RunScript Single line script with Arg

trying to use expression runScript command to run a single l

runScript("args[0].split('/')[-1]",0, {[.]Port Number})

where [.]Port Number = "GigabitEthernet1/0/1"
looking for evaluate to "1"
but doesn't work like this. I suspect it is how I am calling args[0] but not sure how to fix.

Looks like this works but not sure if that is optimal?

runScript("'"+{[.]Port Number}+"'.split('/')[-1]")

In runscript something like this would work. This is inconvenient when passing arguments.

runScript("str.split('some string/some other string','/')",0)

There's also split in the expression language, which I imagine would be easier and more performant.

split("somestring/some other string","/")[1,0]// --> some other string

Perhaps this as another option?

substring({[.]Port Number}, 
  lastIndexOf({[.]Port Number}, "/")+1, 
  len({[.]Port Number})

It is terrible to modify the string that runScript uses. What you are trying to do with the first expression is precisely what my objectScript() expression function supports, including using args in that fashion. (But omitting the polling interval.)

When IA added the ability to pass args to runScript() in v7.8.0 (note the comment in that changelog), they chose to not support one-liners with an args tuples, but instead require the string to name a function or method that would implicitly receive the args tuple.

Your choices are:

  • Move the split operation into a function in your gateway scripting project's script library and call with runScript()'s arg-passing syntax, or

  • Continue using the very non-optimal string contortions, or

  • Install my Simulation Aids module and use objectScript().

  • Use Kevin's expression, but be aware that if {[.]Port Number} changes while the expression is executing, you can get a broken result.

thanks, Using Kevins solution. In this case I am not concerned with changing value since it should be set when first instanced and shouldn't change after that.