Split messages arriving out of order

Well, I think I could have done it easily in REXX which had super strong string processing. But I still got the right answer for today. Though I did, as usual, have to resort to Google a couple of times. Converting "12-15" to a range array was interesting. Though I think that creating range arrays and then comparing them was probably significant overkill. :rofl:

I'm firstly completing them using Snap! and then trying to redo them in NR - I don't know if I'll manage to do Day 3 or not but it keeps me off the streets :slight_smile:

Yes :slight_smile:

Feels like maybe a new design discussion could be started so that people could feed in ideas?

1 Like

Late to the party (busy weekend), but it feels like the batch node ought to help here

Any chance of a demo Dave?

I have never used the batch node & struggle to find a use.

@Steve-Mcl - a simple example like this

[{"id":"a069df4c2d54915d","type":"inject","z":"273b95be5f0a745f","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"abcdef","payloadType":"str","x":110,"y":465,"wires":[["1dd4fa914ea28353"]]},{"id":"1dd4fa914ea28353","type":"split","z":"273b95be5f0a745f","name":"","splt":" 1","spltType":"len","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"topic","x":250,"y":465,"wires":[["1dab94cb6dc24d86"]]},{"id":"1dab94cb6dc24d86","type":"change","z":"273b95be5f0a745f","name":"","rules":[{"t":"delete","p":"parts","pt":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":465,"wires":[["ca860dff7489fd89"]]},{"id":"ca860dff7489fd89","type":"batch","z":"273b95be5f0a745f","name":"","mode":"count","count":"3","overlap":0,"interval":10,"allowEmptySequence":false,"topics":[],"x":580,"y":465,"wires":[["f6bc94c2c794ee5c"]]},{"id":"f6bc94c2c794ee5c","type":"join","z":"273b95be5f0a745f","name":"","mode":"auto","build":"string","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":725,"y":465,"wires":[["a5d91176e36dbaf2"]]},{"id":"a5d91176e36dbaf2","type":"debug","z":"273b95be5f0a745f","name":"split messages","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":895,"y":465,"wires":[]}]

If you actually want two messages with two strings as outputs then remove the change node.

BUT - reading the OP in more detail it seems like the batch size needs to vary - ie to split original string in half - and the batch node doesn't allow the split size to be overridden - so not an exact answer in this instance.

2 Likes

Once split, the split's sequence number can be used in a simple switch node to grab the first x entries to one output and the rest to the other. Then a join node on each output does the rest.

yes true.

[{"id":"f122b814d6465392","type":"inject","z":"273b95be5f0a745f","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"abcdefghijklakl","payloadType":"str","x":140,"y":540,"wires":[["fac0c3dbda45b9af"]]},{"id":"fac0c3dbda45b9af","type":"junction","z":"273b95be5f0a745f","x":240,"y":450,"wires":[["cbb3a65ea57ac006","f414bd7c21411950"]]},{"id":"6b677bd092ac5687","type":"inject","z":"273b95be5f0a745f","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"abcdef","payloadType":"str","x":110,"y":450,"wires":[["fac0c3dbda45b9af"]]},{"id":"06a19c721ea11aac","type":"inject","z":"273b95be5f0a745f","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"abcdefhghic","payloadType":"str","x":130,"y":495,"wires":[["fac0c3dbda45b9af"]]},{"id":"cbb3a65ea57ac006","type":"split","z":"273b95be5f0a745f","name":"","splt":" 1","spltType":"len","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"topic","x":340,"y":495,"wires":[["c294106f1e604818"]]},{"id":"f414bd7c21411950","type":"change","z":"273b95be5f0a745f","name":"","rules":[{"t":"delete","p":"sec","pt":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":450,"wires":[[]]},{"id":"c294106f1e604818","type":"function","z":"273b95be5f0a745f","name":"function 7","func":"msg.split = msg.parts.count/2\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":485,"y":495,"wires":[["f5ce9a3af5f04486"]]},{"id":"f5ce9a3af5f04486","type":"switch","z":"273b95be5f0a745f","name":"","property":"parts.index","propertyType":"msg","rules":[{"t":"lt","v":"split","vt":"msg"},{"t":"else"}],"checkall":"true","repair":true,"outputs":2,"x":640,"y":495,"wires":[["20cc830ca7e93daa"],["0fa681706f955292"]]},{"id":"20cc830ca7e93daa","type":"change","z":"273b95be5f0a745f","name":"","rules":[{"t":"set","p":"sec[msg.payload]","pt":"flow","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":860,"y":480,"wires":[[]]},{"id":"0fa681706f955292","type":"switch","z":"273b95be5f0a745f","name":"","property":"sec","propertyType":"flow","rules":[{"t":"hask","v":"payload","vt":"msg"}],"checkall":"true","repair":false,"outputs":1,"x":805,"y":525,"wires":[["2dbaeceb7d530ef8"]]},{"id":"2dbaeceb7d530ef8","type":"debug","z":"273b95be5f0a745f","name":"debug 14","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":945,"y":525,"wires":[]}]

For amusement, here is my over-complex answer to the first part of that challenge:

const range = (start, stop) => {
    start = Number(start)
    stop = Number(stop)
    return Array.from(
        { length: (stop - start + 1) }, 
        (_, i) => {
            return start + i
        }
    )
}

const out = msg.payload.split(' ')
let trueCount = 0

out.forEach( (pair, i) => {
    let splitPair = pair.split(',')

    splitPair.forEach( (rng, j) => {
        const rngSplit = rng.split('-')
        // Urm, could have just compared the start/end numbers! šŸ˜
        splitPair[j] = range(rngSplit[0], rngSplit[1])
    })
    // out[i] = splitPair

    const trueContains = {
        'pair': pair,
        'All of A in B': splitPair[0].every(r => splitPair[1].includes(r)),
        'All of B in A': splitPair[1].every(r => splitPair[0].includes(r))
    }

    if (trueContains['All of A in B'] || trueContains['All of B in A']) trueCount++

    out[i] = trueContains
})


msg.payload = out
msg.trueCount = trueCount
return msg

where the input was a big multi-line input of paired ranges.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.