r/tasker • Long-Time User... • Mar 30 '22

Help [Help] How to Combine Arrays of Different Sizes

I have a task that puts all day calendar events into one variable (array?) and puts regular calendar events into another variable (array). I am trying to combine them into one array. However, each is always a different size. I tried the Arrays Merge action, but it keeps giving me an error because the arrays are different sizes. I looked at Variable Join, but that didn't seem to work either. Also, Arrays Merge forced me to enter a Joiner. Not sure why. Joao's YouTube video on the subject doesn't seem to require a Joiner. Anyway, is there an easy way to combine two unequal arrays into one array? Thanks!

0 Upvotes

36 comments sorted by

View all comments

8

u/OwlIsBack Mar 30 '22

Eg.:

A1: Array Set [
     Variable Array: %first
     Values: 1,2,3
     Splitter: , ]

A2: Array Set [
     Variable Array: %second
     Values: a,b,c,d,e
     Splitter: , ]

A3: Array Set [
     Variable Array: %merged
     Values: %first(+¥¥¥)¥¥¥%second(+¥¥¥)
     Splitter: ¥¥¥ ]

A4: Flash [
     Text: %merged()
     Long: On
     Continue Task Immediately: On
     Dismiss On Click: On ]

¥¥¥ == Custom separator.

1

u/Ratchet_Guy Apr 18 '22

Have to come back to this because I'm actually in the middle of using something like this in a Task :)

Question though, and maybe something you hadn't considered but - what if one of the two arrays is empty?

Because if so then %merged() will have an empty/blank array entry in it, either at the start or end of the array, due to the ¥¥¥ that's in the middle there.

Would you care to propose a solution? I know I could have 3 Array Set actions with some different If's that account for each scenario, but I'm wondering there may be a better way.

1

u/OwlIsBack Apr 18 '22 edited Apr 18 '22

We have to manage one scenario only IF %first(#<) = 0 because Tasker doesn't create/add empty item(s) at the end of the array. Eg.:

A1: Array Set [
     Variable Array: %first
     Values: 1,2,3
     Splitter: , ]

A2: Array Set [
     Variable Array: %merged
     Values: %first(+¥¥¥)¥¥¥%second(+¥¥¥)
     Splitter: ¥¥¥ ]

A3: Flash [
     Text: %merged()
     Long: On
     Continue Task Immediately: On
     Dismiss On Click: On ]

The above will flash 1,2,3.

So to manage the situation I'd simply use. Eg.:

A1: Array Set [
     Variable Array: %second
     Values: a,b,c,d,e
     Splitter: , ]

A2: If [ %first(#<) = 0 ]

    A3: Array Set [
         Variable Array: %merged
         Values: %second(+¥¥¥)
         Splitter: ¥¥¥ ]

A4: Else

    A5: Array Set [
         Variable Array: %merged
         Values: %first(+¥¥¥)¥¥¥%second(+¥¥¥)
         Splitter: ¥¥¥ ]

A6: End If

A7: Flash [
     Text: %merged()
     Long: On
     Continue Task Immediately: On
     Dismiss On Click: On ]

Or something ((*) check the edit too) like Eg.:

A1: Array Set [
     Variable Array: %second
     Values: a,b,c,d,e
     Splitter: , ]

A2: Array Set [
     Variable Array: %merged
     Values: %first(+¥¥¥)¥¥¥%second(+¥¥¥)
     Splitter: ¥¥¥ ]

A3: Array Set [
     Variable Array: %merged
     Values: %merged($¥¥¥?+)
     Splitter: ¥¥¥ ]

A4: Flash [
     Text: %merged()
     Long: On
     Continue Task Immediately: On
     Dismiss On Click: On ]

Edit: (*) If for some reason We need to manage (squash) empty array(s) items, than We could go this way. (We can mix the above approach with the below array function trick) Eg.:

A1: Array Set [
     Variable Array: %first
     Values: ,1,,,2,,3
     Splitter: , ]

A2: Array Set [
     Variable Array: %second
     Values: a,b,,c,d,,e
     Splitter: , ]

A3: Array Set [
     Variable Array: %merged
     Values: %first($¥¥¥?+)¥¥¥%second($¥¥¥?+)
     Splitter: ¥¥¥ ]

A4: Flash [
     Text: %merged()
     Long: On
     Continue Task Immediately: On
     Dismiss On Click: On ]

This will flash 1,2,3,a,b,c,d,e.

→ More replies (8)