Sorting Response data into order

expression

#1

I’m trying to create a BusWatch type piston that calls the Transport for London API and then tells me how long until the next couple of busses arrive. The problem is that the bus stop I’m looking at has more than one bus route but the response lists all of one bus route then all of the next so not necessarily in the order they will arrive. I would like to be able to re-order the data so that I can get the arrival time (in seconds) of the next two busses and then use a bit of maths to get it into minutes & seconds format.
Count of busses on approach - [7]
Bus Lines on approach - [E2, E2, E7, E7, E8, E8, E8]
Bus arrival times on approach - [783, 1296, 449, 1233, 518, 1485, 1004]
One of the things I’ve been trying is simply to get the min() value from the third list so that I can re-order them and then some how apply the same re-ordering to the Bus Lines

2) What is the expected behaviour?
to sort the data but not entirely sure how yet.

3) What is happening/not happening?
Am unable to even get the min() value from a list generated from the $response data

16/08/2022, 14:54:56 +268ms
+3ms ╔Received event [27Curzon].wc_async_reply = httpRequest with a delay of 0ms, canQueue: true, calledMyself: false
+11ms ║RunTime initialize > 10 LockT > 1ms > r9T > 1ms > pistonT > 0ms (first state access 8 4 6)
+13ms ║Runtime (7373 bytes) initialized in 1ms (v0.3.114.20220418_HE)
+33ms ║╔Execution stage started
+44ms ║║9
+46ms ║║Executed virtual command log (2ms)
+51ms ║║E2
+52ms ║║Executed virtual command log (2ms)
+57ms ║║[E2, E2, E2, E7, E7, E8, E8, E8, E8]
+59ms ║║Executed virtual command log (2ms)
+63ms ║║[1617, 1101, 420, 1307, 523, 775, 1734, 1254, 294]
+65ms ║║Executed virtual command log (2ms)
+73ms ║║[1617, 1101, 420, 1307, 523, 775, 1734, 1254, 294]
+75ms ║║Executed virtual command log (2ms)
+83ms ║║Calculating (integer)14 + (integer)1 >> (integer)15
+88ms ║║Calculating (string)Bus: + (string)E2 >> (string)Bus:E2
+89ms ║║Calculating (string)Bus:E2 + (string),Expected at: >> (string)Bus:E2,Expected at:
+91ms ║║Calculating (string)Bus:E2,Expected at: + (string)15 >> (string)Bus:E2,Expected at:15
+92ms ║║Calculating (string)Bus:E2,Expected at:15 + (string):21 >> (string)Bus:E2,Expected at:15:21
+95ms ║║Bus:E2,Expected at:15:21
+97ms ║║Executed virtual command log (2ms)
+100ms ║╚Execution stage complete. (66ms)
+104ms ╚Event processed successfully (101ms)
16/08/2022, 14:54:55 +907ms
+9ms ╔Received event [27Curzon].test = 1660658095907 with a delay of 0ms, canQueue: true, calledMyself: false
+43ms ║RunTime initialize > 41 LockT > 0ms > r9T > 30ms > pistonT > 29ms (first state access 11 10 31)
+46ms ║Runtime (7292 bytes) initialized in 30ms (v0.3.114.20220418_HE)
+48ms ║╔Execution stage started
+56ms ║║Sending asynchttpGet web request to: api.tfl.gov.uk/Line/e2,e7,e8/Arrivals/490000062F
+58ms ║║Executed virtual command httpRequest (3ms)
+61ms ║║Requesting wake up at Tue, Aug 16 2022 @ 2:55:19 PM BST (in 23999ms) for 1 (st:2)
+67ms ║╚Execution stage complete. (19ms)
+97ms ║Setting up scheduled job for Tue, Aug 16 2022 @ 2:55:19 PM BST (in 24020ms)
+99ms ╚Event processed successfully (91ms)


#2

@E_Sch any chance you can add a new sort() function? :slight_smile:


#3

I made a change in the latest HE webcore, so that the min function as used in the posted piston works.

Should be out in day or so once I do some more testing.


#4

So HPM repair on your HE system to get latest to try.

Here is my adjust test version of your piston


#5

I tried it and it worked for my test on the second try. My first try my variable was set to String, changed it to Dynamic and got the desired result.

image


#6

It does indeed work. Thanks so much for this. I can now get the min time from the list of bus times. Now onto the next stage of using this to sort them. I think I have a plan. I’ll post back if/when I have it working.
Thanks again


#7

If you end up needing sort on an integer[] (or decimal[])

It is likely easy to do as a new function in webcore.

I was not clear on your use so I did not do this.


#8

Thanks both for your help on this so far. So I have had a play with this but basically fallen at the next hurdle. My plan was to use the indexOf() function to find out the position of the min() value.

indexOf($response.timeToStation,min($response.timeToStation)

I can assign the index number to a int variable(index) then use it to extract the correct data

$response.[index].timeToStation
$response.[index].lineName

However I’m can’t get the indexOf() function work. Any Ideas or perhaps a cleaner way of doing this?
I like the idea of a sort function but not sure how useful it would be for this situation as I want to get the same order of data from two places. timeToStation & lineName.

Thanks again


#9

I am not sure yet but I believe the change in min() somehow affected other functions. I have a PM with E_Sch about an existing piston I have that I think may have been due to the change in min().

In my old installation of webCoRE, this one works: (expression) indexOf(times,"294") »»» (integer) 46

New version gives: (expression) indexOf(times,"294") »»» (error) Expecting indexOf(stringOrDeviceOrList, substringOrItem)


#10

indexOf retrieves the character count of a string not the comma delineated position.

You could you use repeat loop to compare…


#11

I noticed an issue with the replace function, unfortunately i didn’t capture the log but the error indicated that the input was not as expected.


#12

@fieldsjm Just after I posted I realized that the indexOf() wouldn’t work. I’ve used your piston code and it’s doing exactly what I was after so thanks for that. Thanks again for everyone’s input.


#13

So just pushed another update to fix reported regression.

I have not tried a test of indexof, but someone can try that or give me a reproducer.

hpm repair to get the update.


#14

count() works as before, thanks!

Sending you my test piston of indexOf()