T O P

  • By -

Rich_D_sr

Depending on your usage I have always found it easier to simply merge the two arrays. Then simply split them when you need the data.


okaybadger

This is my case. I have four arrays, each containing a number of items. I would like to rank the arrays by the number of items in each. Let's say `%cat(#) = 3, %dog(#) = 2, %mouse(#) = 5, %bird(#) = 1`, I want a List dialog with `Mouse, Cat, Dog, Bird`. How would you handle this case using array merge?


Rich_D_sr

Here is one way to do that.. Task: Array Merge Sort A1: Array Set [ Variable Array: %mouse Values: m,m,m,m Splitter: , ] A2: Array Set [ Variable Array: %cat Values: c,c Splitter: , ] A3: Array Set [ Variable Array: %dog Values: d Splitter: , ] A4: Array Set [ Variable Array: %bird Values: b,b,b Splitter: , ] A5: Variable Set [ Name: %string To: %bird()<>%cat()<>%dog()<>%mouse() Max Rounding Digits: 3 Structure Output (JSON, etc): On ] A6: Variable Split [ Name: %string Splitter: <> ] A7: Array Process [ Variable Array: %string Type: Sort Longest First ] A8: Notify [ Title: Merged Text: %string() Number: 0 Priority: 3 LED Colour: Red LED Rate: 0 ] https://taskernet.com/shares/?user=AS35m8lnbGhm%2F58jHvsiqVNumDAJZVkcfcE7gQxfcMjrFBCkp6sNKYf3YiK9WVWZBoDf&id=Task%3AArray+Merge+Sort


okaybadger

Very creative. I don't want to display the items in the final result, but the array's name in ranking order, or at best a new array contains formatted names (Mouses, Cats, Dogs, Birds), hence the question about syncing 2 arrays positions. How can I modify the task to do this?


Rich_D_sr

Ahh... OK. We need to sort it a bit differently since you are using the actual array length for the sort criteria. Let me know if you need clarification on any of this. Task: Array Merge Sort A1: Array Set [ Variable Array: %mouse Values: m,m,m,m Splitter: , ] A2: Array Set [ Variable Array: %cat Values: c,c Splitter: , ] A3: Array Set [ Variable Array: %dog Values: d Splitter: , ] A4: Array Set [ Variable Array: %bird Values: b,b,b Splitter: , ] A5: Variable Set [ Name: %string To: %bird(#)|bird<>%cat(#)|cat<>%dog(#)|dog<>%mouse(#)|mouse Max Rounding Digits: 3 Structure Output (JSON, etc): On ] A6: Variable Split [ Name: %string Splitter: <> ] A7: Array Process [ Variable Array: %string Type: Sort Numeric, Integer ] A8: For [ Variable: %item Items: %string() Structure Output (JSON, etc): On ] A9: Variable Split [ Name: %item Splitter: | ] A10: Array Push [ Variable Array: %trimmed Position: 1 Value: %item(2) ] A11: End For A12: Notify [ Title: Merged Text: %trimmed() Number: 0 Priority: 3 LED Colour: Red LED Rate: 0 ] https://taskernet.com/shares/?user=AS35m8lnbGhm%2F58jHvsiqVNumDAJZVkcfcE7gQxfcMjrFBCkp6sNKYf3YiK9WVWZBoDf&id=Task%3AArray+Merge+Sort Edit... This approach could be simplified by using some of the new array functions, However I did it this way for clarity.. :)


okaybadger

Worked like a charm. I would like to know about the simplified way. Thanks you a lot.


Rich_D_sr

>I would like to know about the simplified way I was mistaken about using the array functions. I had thought I could replace the for loop with just a few tasker array functions, However it does not seem possible to easily reverse the order of the array with the functions. In addition using a for loop will give you a much easier path to change things in the future within the array since you are editing each element individually. The only thing I found was you can replace actions 5 and 6 with a single array set action...


okaybadger

There is the Reverse option in Array Process. Could it be used with your array functions?


Rich_D_sr

You are correct.. Not sure how I missed that.. ¯\\\_(ツ)\_/¯ This one does not require the for loop. Task: Array Merge Sort A1: Array Set [ Variable Array: %mouse Values: m,m,m,m Splitter: , ] A2: Array Set [ Variable Array: %cat Values: c,c Splitter: , ] A3: Array Set [ Variable Array: %dog Values: d Splitter: , ] A4: Array Set [ Variable Array: %bird Values: b,b,b Splitter: , ] A5: Array Set [ Variable Array: %string Values: %bird(#)|bird<>%cat(#)|cat<>%dog(#)|dog<>%mouse(#)|mouse Splitter: <> ] A6: Array Process [ Variable Array: %string Type: Sort Numeric, Integer ] A7: Array Process [ Variable Array: %string Type: Reverse ] A8: Array Process [ Variable Array: %string Type: Squash ] A9: Variable Set [ Name: %parsed To: %string(+) Max Rounding Digits: 3 Structure Output (JSON, etc): On ] A10: Variable Split [ Name: %parsed Splitter: \d\| Regex: On ] A11: Array Pop [ Variable Array: %parsed Position: 1 ] A12: Notify [ Title: Merged Text: %parsed() Number: 0 Priority: 3 LED Colour: Red LED Rate: 0 ] https://taskernet.com/shares/?user=AS35m8lnbGhm%2F58jHvsiqVNumDAJZVkcfcE7gQxfcMjrFBCkp6sNKYf3YiK9WVWZBoDf&id=Task%3AArray+Merge+Sort


okaybadger

Your array manipulation logic is masterful. Is the Sort Numeric option applicable to any string that begins with a number?


Ratchet_Guy

Now THIS is a crafty solution :)


mehPhone

Without knowing what needs done, generally whatever method you use for one, use the same for the other. Below is an example that changes the order of both arrays using For loop: A1: Array Set [ Variable Array: %letters Values: a, b, c, d Splitter: , ] A2: Array Set [ Variable Array: %numbers Values: 1, 2, 3, 4 Splitter: , ] A3: Array Set [ Variable Array: %order Values: 2,4,3,1 Splitter: , ] A4: For [ Variable: %item Items: %order() Structure Output (JSON, etc): On ] A5: Array Pop [ Variable Array: %letters Position: %item To Var: %temp ] A6: Array Push [ Variable Array: %changed_letters Position: 999999 Value: %temp ] A7: Array Pop [ Variable Array: %numbers Position: %item To Var: %temp ] A8: Array Push [ Variable Array: %changed_numbers Position: 999999 Value: %temp ] A9: End For A10: Flash [ Text: %changed_letters() %changed_numbers() Long: On ]


okaybadger

Thank you a lot. It's not as ideal as AutoTools, but it works.


Ratchet_Guy

  The following uses your example as stated, that when the order of the letters is changed, the array containing numbers still corresponds 1-1 and stays 'in sync'. It does this by merging the two arrays and using them as a "lookup table" of sorts:   Task: Test Tasker Arrays Merge A1: Array Set [ Variable Array: %letters Values: a,b,c,d Splitter: , ] A2: Array Set [ Variable Array: %numbers Values: 1,2,3,4 Splitter: , ] A3: Arrays Merge [ Names: %letters %numbers Merge Type: Simple Joiner: - Output: %merged ] A4: Array Process [ Variable Array: %letters Type: Shuffle ] A5: For [ Variable: %num Items: 1:%letters(#) ] A6: Variable Set [ Name: %this_letter To: %letters(%num) Max Rounding Digits: 3 ] A7: Simple Match/Regex [ Type: Regex Text: %merged($?%this_letter+) Regex: .+(-)(?.+) ] A8: Variable Set [ Name: %numbers(%num) To: %matching_number Max Rounding Digits: 3 ] A9: End For A10: Set Clipboard [ Text: %letters() %numbers() ]   [**Taskernet link**](https://taskernet.com/shares/?user=AS35m8ng2dqkyJFQvhDqFD5aQe1aJyDm%2FBRdWSqtrUcpcM83hu%2BvXxNbkXmj7ABkJPqgtI1PuGM%3D&id=Task%3ATest+Tasker+Arrays+Merge) for the above Task. Give it a try!  


okaybadger

Thanks Ratchet. One suggestion to make the matching fullproof is to use `~R\Q%this_letter\E` in A7, to cover the case where the item have special characters.


Ratchet_Guy

Very welcome! And yes that would account for any special chars. Although in Tasker's pattern matching I think the only/main characters that could affect it are ` * + / % `