Jump to content

Return list of string as parameter


photo

Recommended Posts

Hello

I'm trying to call a function and return a list of element, but the list always return with the element were each element is null. Here is a sample with a list of string, I try many variant but don't work any. 

 

    void btTest()
    {
        string test[];
        teststr(test);
        log.message("\n"); 
        foreach(string st; test)
        {
           log.message(format("-> %s", st)); 
        }
    }

    void teststr(string &strs[])
    {
        for (int i=0;i<20;i++)
        {
            string s = format(" %d,", i);
            strs.append(s);
            log.message(s); 
        }
    }
 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
-> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> -> 

Any idea how I can return a array of elements from a function using Uniginescript

 

Link to comment

I change strs.append(s)  -- > strs = s; and this work. look like the append not is working correctly or I miss something.  

The code now with the console will be: 

    void btTest()
    {
        string test[];
        teststr(test);
        log.message("\n"); 
        foreach(string st; test)
        {
           log.message(format("-> %s", st)); 
        }
    }

    void teststr(string &strs[])
    {
        for (int i=0;i<20;i++)
        {
            string s = format(" %d,", i);
            strs[i] = s;
            log.message(s); 
        }
    }
 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
->  0,->  1,->  2,->  3,->  4,->  5,->  6,->  7,->  8,->  9,->  10,->  11,->  12,->  13,->  14,->  15,->  16,->  17,->  18,->  19,

 

Link to comment

Hi Roberto,

The point is that when declaring an empty vector, you are required to specify its size (see the article about Containers):

int vector[0];

So, this code should work properly:

    void btTest()
    {
        string test[0]; // <-- specify zero-size for an empty vector
        teststr(test);
        log.message("\n"); 
        foreach(string st; test)
        {
           log.message(format("-> %s", st)); 
        }
    }

    void teststr(string &strs[])
    {
        for (int i=0;i<20;i++)
        {
            string s = format(" %d,", i);
            //strs[i] = s;
			strs.append(s);
            log.message(s); 
        }
    }

Thanks!

  • Like 1
Link to comment
×
×
  • Create New...