Page 1 of 1

split function

Posted: 26 Oct 2017 16:05
by digitalstone
I have been banging my head against the metaphoric wall for 2 days, so i'm placing this (supposedly) easy question here.

I have some raw weather information From this website:

000|17:15
000|17:20
000|17:25
000|17:30
000|17:35
000|17:40
000|17:45
000|17:50
000|17:55
000|18:00
000|18:05
000|18:10
000|18:15
000|18:20
000|18:25
000|18:30
000|18:35
000|18:40
000|18:45
000|18:50
000|18:55
000|19:00
000|19:05
000|19:10

Now, i can break down this whole chunk to separate lines with split(text, "\n").
The problem starts when breaking down further on the "|" and spaces (there are spaces at the end of each line) with any variation i tried like

Code: Select all

split(text_line, "|*\\s")
Even using just "|" alone won't even filter the thing out.
My script (1 of many delimiter variations i've tried):

Code: Select all

// Get lines from rain table
lineSplit =split(text,"\n\s");

// Get rain value + time value
lineTime = split(lineSplit, "|")[0];
lineTime = split(lineSplit, "|")[1];

Re: split function

Posted: 26 Oct 2017 17:19
by yogi108
Hi,

The problem is the special char '|'
You have to escape it like this

linesplit=split(line,'\\|');

After that you can trim(linesplit[1]);
To remove the whitespaces

Regards,

Re: split function

Posted: 26 Oct 2017 17:30
by digitalstone
Oh.. my god... yep i should have known :roll:
Thanks a bunch Yogi!

Re: split function

Posted: 27 Oct 2017 01:50
by Desmanto
Just as alternative. Since 1.34.0, I never use split() function anymore. findAll() has all I need, and usually it is so powerful, that most splitting can be done in single line of script (thanks Martin, for this very useful addition).

For this case, you wanna extract the "000" and "17:15" as separate list. I would use

Code: Select all

find = findAll(text_line, '(\\d{3})\\|(\\d{2}:\\d{2})', true);
The rain value can be access from all first group capture and time from second group capture. (try to use debug dialog to see the result of capture).

In case you wanna get the first forecast only, you can use

Code: Select all

rain = find[0][1];
time = find[0][2];
It will give you rain = "000" and time = "17:15". You have 24 lines, so to access the last line, you can use find[23][1] and find[23][2]. Or if the data is indefinite, you have to check the length of {find} using length() and -1 (last index).

You can use built-in regex tester to test your regex. You can see the result and the proper regex syntax needed for each function.

Re: split function

Posted: 30 Oct 2017 19:00
by digitalstone
Desmanto wrote:Just as alternative. Since 1.34.0, I never use split() function anymore. findAll() has all I need, and usually it is so powerful, that most splitting can be done in single line of script (thanks Martin, for this very useful addition).

For this case, you wanna extract the "000" and "17:15" as separate list. I would use

Code: Select all

find = findAll(text_line, '(\\d{3})\\|(\\d{2}:\\d{2})', true);
The rain value can be access from all first group capture and time from second group capture. (try to use debug dialog to see the result of capture).

In case you wanna get the first forecast only, you can use

Code: Select all

rain = find[0][1];
time = find[0][2];
It will give you rain = "000" and time = "17:15". You have 24 lines, so to access the last line, you can use find[23][1] and find[23][2]. Or if the data is indefinite, you have to check the length of {find} using length() and -1 (last index).

You can use built-in regex tester to test your regex. You can see the result and the proper regex syntax needed for each function.
Very nice alternative.
Although i've already implemented the split function, i still need to make it a lot more neat once i'm done with every other thing.
I think i'll take a look at the findAll() function. Thanks :)