Noticed an issue with the split command

The agent was trying to split this string with 2 commas in a row. It was an accidental incident but I am surprised it caused an error.
`
2014-02-19 21:47:33 UTC-6: [Agent] 0.971254,40192,-32768,131,30,36,0
2014-02-19 21:47:33 UTC-6: [Agent] bad parameters to split(string, string)
2014-02-19 21:47:33 UTC-6: [Agent] at send_xively:171

line 171: d = split(body,",");

`

Are you sure body is a string, and that body is what you think it is?

Here’s a really quick test I whipped up:

function test() { body <- "0.971254,40192,-32768,,131,30,36,0"; server.log(body); d <- split(body,","); foreach(k,v in d) { server.log(k +": " + v); } } test()

and here’s the output:

2014-02-20 08:31:11 UTC-8: [Agent] 0.971254,40192,-32768,,131,30,36,0 2014-02-20 08:31:11 UTC-8: [Agent] 0: 0.971254 2014-02-20 08:31:11 UTC-8: [Agent] 1: 40192 2014-02-20 08:31:11 UTC-8: [Agent] 2: -32768 2014-02-20 08:31:11 UTC-8: [Agent] 3: 131 2014-02-20 08:31:11 UTC-8: [Agent] 4: 30 2014-02-20 08:31:11 UTC-8: [Agent] 5: 36 2014-02-20 08:31:11 UTC-8: [Agent] 6: 0

…though I’m not sure that looks correct to me either. Surely index 3 should be an empty string?

If you’re using it for CSV, then you might well have expected the empty string. But Squirrel’s string.split is defined to work like C’s strtok, which collapses multiple separators into one. To make that seem less unreasonable, imagine splitting “a sentence of text” into words: you’d want multiple spaces to act the same as one.

Peter

Oh, absolutely fair. Been a while since I used strtok!