Highlighted
Established Member..
Established Member..
3526 views

converting an array to a string...

Jump to solution
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...
1 Solution

Accepted Solutions
Highlighted
Absent Member.
Absent Member.

Re: converting an array to a string...

Jump to solution
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")

View solution in original post

9 Replies
Highlighted
Absent Member.
Absent Member.

Re: converting an array to a string...

Jump to solution
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
0 Likes
Highlighted
Established Member..
Established Member..

Re: converting an array to a string...

Jump to solution
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
0 Likes
Highlighted
Absent Member.
Absent Member.

Re: converting an array to a string...

Jump to solution
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
0 Likes
Highlighted
Absent Member.
Absent Member.

Re: converting an array to a string...

Jump to solution
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")

View solution in original post

Highlighted
Established Member..
Established Member..

Re: converting an array to a string...

Jump to solution
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.
0 Likes
Highlighted
Contributor.. Contributor..
Contributor..

Re: converting an array to a string...

Jump to solution

Hi All, 

I have one requirement for writing a validation for 'Solution' field. When solution is written less than 25 characters, as validation should throw saying" Solution should be greater than 25 characters".  I am using currently HPSm 9.40 version. Need to write conditions in RuleSets. Please help.

I have one issue here. Solution field is of Array type. How to conevrt Array to Character. I have written the below condition for conversion. 

vars.$L_file.resolution.toString();
print(vars.$L_file.resolution);  

and it is printing like {"1","2","3"}  .  But, I want this to come in 123...like no.of characters. 

Also,After this, I am not knowing how to write a validation after this.Please help me. I am not aware of the coding part.

Hi Keven/Cliff, I have tried to write RAD function like you have provided. But, couldn't make it. Could u please help. Here the input value for Solution field is 'resolution'. 

 

0 Likes
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

Re: converting an array to a string...

Jump to solution

There is create solutions above.

Also you can use next JS to convert array to string:

function ArrayToString(val)
{
	var type=system.functions.type(val)
	if (type==8){
		return val.join("")
	}
}
0 Likes
Highlighted
Contributor.. Contributor..
Contributor..

Re: converting an array to a string...

Jump to solution
Hi sir, I have written one JS code as below and it is calculating the no.of characters. Now I am not knowing how to use this script for writing a validation for solution field by restricting the length as "Please provide minimum 25 characters". Please help on this sir. var resolution=vars.$L_file.resolution var resolutionString= ""; for(i in resolution) { if (resolution[i] == null || resolution[i] == "") { } else { resolutionString = resolutionString + " " + resolution[i]; } } var resolutionLength = resolutionString.length
0 Likes
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

Re: converting an array to a string...

Jump to solution

You need use FormatControl for your table (incidents or probsummary)

In Validations tab:

for ADD (if sombody will write resolution when adding ticket or leave it empty if you need check it only when status chenged to resolved)

and for UPDATE set true or needed condition,

for example: not same(status in $file, status in $file0) and status in $file="Resolved" 

For VALIDATION:

jscall("YourLibName.CalcStringLength", resolution in $file) >25

And function in SL something like:

 

function CalcStringLength(val)
{
var type=system.functions.type(val)
if (type==8)
{
val=val.join()
}
return val.length
}

 

 

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.