[SOLVED] Script with variable arguments passes arguments to second script - how?

thaaks

Member
Hi,

I have a script that looks like this:
Code:
///foo(key,messageWithPlaceHolders,arguments)
var key = argument[0];
var msg = argument[1];
// some smart message parsing code here to add the remaining arguments into the message
where I replace parts inside the message with the following arguments, starting at argument[2].

Now I want to have another script that can be called without the key but uses a preset one. To avoid code redundancy I simply want to call the existing script foo.
Code:
///foobar(messageWithPlaceHolders,arguments)
var key = "whatever";
var msg = argument[0];
foo(key, msg, ???); // how to pass the remaining arguments into foo???
Is there a smart way to pass the remaining arguments to foo, no matter how many arguments there are?
 

FrostyCat

Member
Not any that I know of, now that the argument limit has been lifted and hard-coding the 17 possibilities as a utility script is no longer a cover-all.

One way to get this working is to pass in an array as a single argument instead of each entry in its own argument.
 
T

The5thElement

Guest
You could try using For Loop and a Switch statement to check how many arguments where passed.

Code:
var argsCount = argument_count;
var args;

for(i = 0; i < 5; i++){
     if(argsCount > i){
          args[i] = argument[i];
     }
}

switch(argsCount)
{
     Case 1:
     {
          //Run script with argument[args[0]]  and any other arguments at a default value.
          foo(key, msg, args[0], value1, value2, value3, value4);
          break;
     }
     Case 2:
     {
          //Run script with argument[args[0]] and argument[args[1]]   and any others at a default value.
          foo(key, msg, args[0], args[1], value2, value3, value4);
          break;
     }
     default:
     {
          //Run script with all arguments set at a default value.
          foo(key, msg, value0, value1, value2, value3, value4);
          break;
     }
}
Edit: Added more to the example.
 
Last edited by a moderator:

thaaks

Member
Hi guys,
thanks for your answers!
I will go for FrostyCat's suggestion. Using an array simplifies dealing with the arbitrary arguments a lot. The additional parameters are passed as an array, so codewise pretty similar to argument[x].
My function foobar() can pass the array to foo(), no hassle with argument counting and so on.
And the well known array() script will solve all difficulties for the calling code to wrap up the parameters.
Code:
///array(val1,val2,val3,...,valn)
var a;
for (var i=0;i<argument_count;i++){
    a[i] =argument[i];
}
return a;
So I can do
Code:
foo("myKey", "myMessageWithPlaceHolders", array("arg1", arg2, arg3));
and also in script/function foobar I can do
Code:
///foobar(messageWithPlaceHolders,argumentsAsArray)
var key = "whatever";
var msg = argument[0];
var args = argument[1];
foo(key, msg, args);
That solves my problem perfectly. Thanks!
 
Top