Respected Contributor.. relent0r Respected Contributor..
Respected Contributor..
461 views

How do integers get passed into scriptlets?

Hi All

I have a requirement to validate an integer.

The use case is that I get passed two inputs for a SCSI bus and unit. Then use them to define the scsi id of a disk being added to a vmware vm, if they are not integers then pass a blank variable so that the operation will autoassign the scsi id.

Then what I 'was' going to do was using a scriptlet validate if they were integers and if so create a new variable with a : between them.

so if bus = 3 and unit =5

scsi = 3:5

and if bus = "b" and unit = "c"

scsi = ""

But when I get these from a scriptlet its like they are now strings.

The scriptlet is below. If I set diskBus and diskUnit to a number in the script life is good. But if I use the inputs disk_bus and disk_unit then it won't see them as integers and the result of the if statement is always false. Can someone tell me what I'm doing wrong here?

 

var diskBus = disk_bus;
var diskUnit = disk_unit;
var isInt = function (n) { return n === (n | 0); };
var test;
var diskSCSI;

if (isInt(diskBus) && isInt(diskUnit))
{
test = "true";
diskSCSI = diskBus + ':' + diskUnit;
}
else
{
test = "false";
diskSCSI = "";
}

scriptletContext.put("disk_scsi",diskSCSI); 
scriptletResult = diskBus + diskUnit + diskSCSI + test;

 

Labels (2)
0 Likes
2 Replies
Established Member.. TerrySummers
Established Member..

Re: How do integers get passed into scriptlets?

You can attempt to convert a string into a number using the Number function.

var test=Number(test);

I'd wrap it in a Try, Catch so if it in not a number you can take appropiate action.

0 Likes
Respected Contributor.. relent0r Respected Contributor..
Respected Contributor..

Re: How do integers get passed into scriptlets?

I opted to use a regex function to check a string which is fine. Was just suprised that it didn't maintain the type as I thought if you wanted a number as a string you had to put quotes around it in the input field.

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.