How to: convert array into string?

Get Help on scripting in POL with configurations, config setups, script trouble shooting, etc.

Moderator: POL Developer

Post Reply
Harley
Forum Regular
Posts: 353
Joined: Sat Mar 18, 2006 1:41 am
Location: Germany

How to: convert array into string?

Post by Harley » Sun Apr 21, 2019 1:47 pm

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!

DevGIB
Grandmaster Poster
Posts: 232
Joined: Mon Feb 06, 2006 6:12 am

Re: How to: convert array into string?

Post by DevGIB » Mon Apr 22, 2019 5:44 am

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.

Code: Select all

var arr := array{"abcd","123",456};
will save as:

Code: Select all

{ abc, 123, 456 }
For example:

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

	

Harley
Forum Regular
Posts: 353
Joined: Sat Mar 18, 2006 1:41 am
Location: Germany

Re: How to: convert array into string?

Post by Harley » Mon Apr 22, 2019 6:02 am

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.

Code: Select all

	var list := array{ 10, "asdf", 1.0, chr, 44};
	var mystr := "";
	foreach elem in list
		mystr := mystr+" "+CStr(elem);
	endforeach
So, it works very well & very fast!

Yukiko
Distro Developer
Posts: 2694
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Re: How to: convert array into string?

Post by Yukiko » Mon Apr 22, 2019 7:49 am

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/

Turley
POL Developer
Posts: 657
Joined: Sun Feb 05, 2006 4:45 am

Re: How to: convert array into string?

Post by Turley » Mon Apr 22, 2019 8:06 am

Code: Select all

", ".join(myarray)
Will do the trick

Harley
Forum Regular
Posts: 353
Joined: Sat Mar 18, 2006 1:41 am
Location: Germany

Re: How to: convert array into string?

Post by Harley » Mon Apr 22, 2019 11:42 am

Turley wrote:
Mon Apr 22, 2019 8:06 am

Code: Select all

", ".join(myarray)
Will do the trick
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

Yukiko
Distro Developer
Posts: 2694
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Re: How to: convert array into string?

Post by Yukiko » Mon Apr 22, 2019 2:50 pm

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 :)

Post Reply