I have a very basic problem with a slice function…
I am trying out different things and now I’m testing the webcolor function, but with my own RGB LED setup. However, I cannot seem to pass the correct values… First of all, the code in webcolor accepts the HTTP value as “color” - is that correct? Shouldn’t it be rather string function, as we try to slice it further on in the code?
Then I tried to do very basic changes - I’ve changed the HTML code to input “string” rather than color, deleted and added a new node which passes string to my code and tried to return the sliced value:
`class RGBcolor extends InputPort
{
name = "RGB"
type = “string”
function set(col) {
server.log(col.slice(1,3));
}
}`
That code fails with:
3 luty 2013 23:10:21: ERROR: the index ‘slice’ does not exist
3 luty 2013 23:10:21: ERROR: at set:44
Code server.log(col); returns the value correctly… What am I doing wrong?
Oh, I see. “FF0000” comes through as string, “000000” comes as integer. I can prepend it with a character and analyze the rest… Any other way to make it explicit?
No, no way to make it explicit (once it’s converted to an int, you can’t get back to what it was as a string). Again, agents make this easier.
The slice (1,3) is returning the right value. Strings are zero-indexed and the end value is not included, so you should expect to see a 2 character string “12” returned.
See squirrel docs:
slice(start,[end])
returns a section of the array as new array. Copies from start to the end (not included). If start is negative the index is calculated as length + start, if end is negative the index is calculated as length + end. If end is omitted end is equal to the array length.
I missed that part about zero index - looking at the webcolor code I figured it started with one (I figured wrong of course - the web input starts with # and I missed that too).