split function

Post your questions and help other users.

Moderator: Martin

Post Reply
User avatar
digitalstone
Posts: 342
Joined: 21 Oct 2017 12:36
Location: The Netherlands

split function

Post by digitalstone » 26 Oct 2017 16:05

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];
Phone: LG Nexus 5X (rooted vanilla Android 7.1.2)

User avatar
yogi108
Posts: 100
Joined: 09 Nov 2016 08:00
Contact:

Re: split function

Post by yogi108 » 26 Oct 2017 17:19

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,
"You cannot know the meaning of your life until you are connected to the power that created you.”
Shri Mataji Nirmala Devi, founder of Sahaja Yoga

User avatar
digitalstone
Posts: 342
Joined: 21 Oct 2017 12:36
Location: The Netherlands

Re: split function

Post by digitalstone » 26 Oct 2017 17:30

Oh.. my god... yep i should have known :roll:
Thanks a bunch Yogi!
Phone: LG Nexus 5X (rooted vanilla Android 7.1.2)

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: split function

Post by Desmanto » 27 Oct 2017 01:50

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.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

User avatar
digitalstone
Posts: 342
Joined: 21 Oct 2017 12:36
Location: The Netherlands

Re: split function

Post by digitalstone » 30 Oct 2017 19:00

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 :)
Phone: LG Nexus 5X (rooted vanilla Android 7.1.2)

Post Reply