Re: converting an array to a string... (431 Views)
Reply
Honored Contributor
Cliff Cottam
Posts: 579
Registered: ‎07-18-2006
Message 1 of 6 (431 Views)
Accepted Solution

converting an array to a string...

Is there an easy way to convert an array to a string? I am wanting to convert and array like {"This", "is", "an", "array"} to "This is an array". This would be similar to the str() or val() or recordtostring() functions but would work on an array.

I am sure it can be done with a a loop and a few strrep() statements but if there is a simplier way...
Please use plain text.
Honored Contributor
Ben Vargas
Posts: 335
Registered: ‎07-18-2006
Message 2 of 6 (431 Views)

Re: converting an array to a string...

I'm not immediately aware of any single function to accomplish that so either looping or a couple functions together might be your best option, especially since you want to insert spaces between some array elements.

Maybe do three steps rather than a loop:
1. Convert array to string -- str()
2. Clip off the {" and "} -- strclpl() and strclpr()
3. Replace all ", " with " " -- strrep()
Sr. ServiceCenter & Service Manager Consultant
Please use plain text.
Honored Contributor
mark_t_robinson
Posts: 559
Registered: ‎08-03-2006
Message 3 of 6 (431 Views)

Re: converting an array to a string...

Hi Cliff,

The only other thing I could think of is...

$G.thearray={"This", "is", "an", "array"};$G.thestr="";for $i = 1 to lng(denull($G.thearray)) do ($G.thestr=$G.thestr+" "+$i in $G.thearray)

hope this helps...
-Mark
Please use plain text.
Occasional Advisor
Sam Ryburn
Posts: 7
Registered: ‎07-27-2006
Message 4 of 6 (431 Views)

Re: converting an array to a string...

I don't know of a simplier way in SC system language, but if you can use javascript you may be able to use the .join method on the array. The syntax is:
outputStr = inputArray.join( delimiter );

If you wrote a little javascript function you could use it in SC expressions.
function ats( scArray, scDelim )
{
var newstuff = scArray.join( scDelim );
return newstuff;
}

Call the js function in an expression like this:
$L.newstr=jscall("test.ats",$L.array," ")

"test" in "test.ats" would be the name of the ScriptLibrary record.

Good luck!
Sam
Please use plain text.
Honored Contributor
Kevin Seymour
Posts: 637
Registered: ‎07-18-2006
Message 5 of 6 (431 Views)

Re: converting an array to a string...

There is a rad function not generally documented called strraw

Can be used like this......

$L.message={"line 1", "line 2", "line 3", "etc"}

$L.string.message=strraw($L.message)

If you want carriage returns in your string you can use this.......

$L.string.message=strraw($L.message, "newline")
Please use plain text.
Honored Contributor
Cliff Cottam
Posts: 579
Registered: ‎07-18-2006
Message 6 of 6 (431 Views)

Re: converting an array to a string...

All excellent suggestions. Thanks to each of you. Ben and Mark did things about as I would have. Sam gets a few extra point for pusing Java - something I should learn. Keven get 10 points for to solution I thought was avaialble but could not find and the quickest, simplest solution available.
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation