splitCSVRecord() vs splitCSVRecords()

General discussions about Automagic and automation in general

Moderator: Martin

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

splitCSVRecord() vs splitCSVRecords()

Post by Desmanto » 23 Nov 2019 14:19

I just got confused today, when trying to help other guys in the other forum. He has excel file that has the list of customer and its code he want to retrieve. So I tried to make a flow to help him and describe how to do it. During the spliting, I use my old method here : viewtopic.php?f=5&t=7864&p=23549&hilit= ... ord#p23549
splitCSVRecord() will split the single line text into a single level list. Since his list contains multiple lines, I have to split first using split(csv, "\\r?\\n"), then splitCSVRecord() upon the loop, and add it to the new Map. \\r? part is to handle windows' newline break.

However as I tested, the result of the split is always a nested list, not single level. This throw me an error when referring to the second element. So instead of
[1,Aneka Abadi,123456,Jalan Bakung no 12,A]
I got it nested
[[1,Aneka Abadi,123456,Jalan Bakung no 12,A]]
If I dig down the list using debug dialog, I can see the list is stored in nested list clearly. So I wonder what is the difference.

I copied the csv raw data and tried to replicate it in other script element. I got the result correct, it is single level list. Then I repeat the same at the main flow, still got nested list. I tried split(j, ",") to split it manually using comma, I got the result correct, single level list. So what the heck happened, why I can't use the usual splitCSV here?

I am drowsy and fell into sleep. After wake up several hours later, my mind still directed to this. I then mirrored my phone to PC and pay attention properly to see what is the real difference. Finally I saw it!!! The splitCSVRecords() in the main flow have extra "s". While the one in my test script and other flows I usually use, don't have extra "s". So it is splitCSVRecords() vs splitCSVRecord(). It seems I type the script manually in main script and use function button in test script. As in the function button, the only available is the one without extra "s". This explain why I got single vs nested.

In summary
splitCSVRecord() = split single lines of csv into single level list
splitCSVRecords() = split multiple lines of csv into nested level list.

This is such as breakthru, that my 2 years+ with automagic seems like a lie. Taking the example from other thread, I used to use something like this.

Code: Select all

csv = '"Hope, AR, USA",33.669964,-93.590096\n' +
'"Tujunga, Los Angeles, CA, USA",34.263302,-118.302711\n'+
'"Bonnybridge, Scotland, UK",56.003227,-3.888634\n' +
'"Tarzana, Los Angeles, CA, USA",34.150879,-118.551651' ;

temp = split(csv, "\n");
database = newList();
for(i in temp)
  addElement(database, splitCSVRecord(i));
So I have to split the csv first to multiple lines, then splitCSVRecord(). I even have to used temporary variable to do it.

Now with the splitCSVRecords(), I can do it in one line script.

Code: Select all

csv = '"Hope, AR, USA",33.669964,-93.590096\n' +
'"Tujunga, Los Angeles, CA, USA",34.263302,-118.302711\n'+
'"Bonnybridge, Scotland, UK",56.003227,-3.888634\n' +
'"Tarzana, Los Angeles, CA, USA",34.150879,-118.551651' ;

database = splitCSVRecords(csv);
This handles the line break perfectly! This also answer my question on how to handle the csv file which have line break in the element. I never have to use it though, but our office store company address in multi line format, so we can print it neatly. If I use split() with manual "\n", this will definitely break the address into multiple element, which is wrong. Using splitCSVRecords(), the line break inside element is handled properly, not broken into seperate list.

Now I am going to slim down some of my flows which use the splitCSVRecord(), to make it shorter and more efficient.

@Martin : I think we should have the splitCSVRecord() and splitCSVRecords() mentioned both in the function button.
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
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: splitCSVRecord() vs splitCSVRecords()

Post by Martin » 25 Nov 2019 20:04

Sorry about that! The version with the s (splitCSVRecords) was only available in the somewhat hidden list of unofficial functions that's not meant to be available in the regular function list.
I usually do this for functions that I might want to change. I've just moved the function to the official list since I don't have any plans to change it in the near future.

Regards,
Martin

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

Re: splitCSVRecord() vs splitCSVRecords()

Post by Desmanto » 26 Nov 2019 06:24

Oh, so it is still hidden unofficial function. No problem, It is to thru something like this I discover many of my scripting technique until now. So it was still a great exercise. I think the splitCSVRecords() version shouldn't change much anymore, unless you maybe you want to add additional argument to specify the delimiter other than comma. But that will become regular split() anyway :D So it seems to be final then, just leave it as current one and move it to official 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.

Post Reply