Subj!
I didn't found any examples here:
https://docs.polserver.com/pol100/guide ... criptguide
Didn't find any function here:
https://docs.polserver.com/pol100/
And need your help!
I have an array of 2-3 indexes. And I have to convert this array into string and than assign to variable.
With best regards!
How to: convert array into string?
Moderator: POL Developer
Re: How to: convert array into string?
https://docs.polserver.com/pol100/fullf ... sicem#CStr
it accepts an array despite the docs saying it only takes a real or int.
"stringified" arrays will still contain the curly braces e.g.
will save as:
For example:
it accepts an array despite the docs saying it only takes a real or int.
"stringified" arrays will still contain the curly braces e.g.
Code: Select all
var arr := array{"abcd","123",456};
Code: Select all
{ abc, 123, 456 }
Code: Select all
use uo;
use basic;
program textcmd_ArrayTest(who, unused text)
var arr := array{"abcd","123",456};
broadcast(arr);
who.setprop("arraytest1",arr);
who.setprop("arraytest2",cstr(arr));
endprogram
Re: How to: convert array into string?
DevGIB, thanks for your answer, but you are not fully right.
CStr() doesn't convert array into string.
It will be also array with { }.
For example, in PHP, there is a function implode( string $glue , array $pieces )
which convert all objects in array to str format with delimiter ($glue).
In our script, my friend Guilherme told me to use foreach.
So, it works very well & very fast!
CStr() doesn't convert array into string.
It will be also array with { }.
For example, in PHP, there is a function implode( string $glue , array $pieces )
which convert all objects in array to str format with delimiter ($glue).
In our script, my friend Guilherme told me to use foreach.
Code: Select all
var list := array{ 10, "asdf", 1.0, chr, 44};
var mystr := "";
foreach elem in list
mystr := mystr+" "+CStr(elem);
endforeach
Re: How to: convert array into string?
I believe in the Distro there is a user function to implode an array, look in /scripts/include/. If I remember correctly you will find it in arrays.inc or string.inc
https://api.github.com/repos/PolServer/ ... o/zipball/
https://api.github.com/repos/PolServer/ ... o/zipball/
Re: How to: convert array into string?
Code: Select all
", ".join(myarray)
Re: How to: convert array into string?
Geniously!!! Thank you, Turley!
Yukiko, yeap! There is also the same function with foreach:
Code: Select all
/*
* ImplodeArray(glue, array)
*
* Purpose
* Returns a string containing the array elements in the same order with
* the 'glue' string in between each array element.
*
* Parameters
* glue: String to place between each array element.
* array: Array to turn into a string.
*
* Return Value
* Returns a string on success.
* Returns an error on failure.
*
*/
function ImplodeArray(glue, byref the_array)
if ( Lower(TypeOf(the_array)) != "array" )
return error{"errortext":="Object passed was not an array."};
endif
var str_array := "";
foreach element in ( the_array )
str_array += (CStr(element)+glue);
SleepMS(2);
endforeach
return str_array;
endfunction
Re: How to: convert array into string?
Thanks Turley. I didn't know that method was there. I guess it would be a good idea to snuggle up in my comfy chair with a cold beer at hand and spend a quiet evening reading through the docs