How to split and format to avoid a loop?

Hi,
I need help to do the right split & format with 2 different messages, as the second msg is also including the first.

The issue : the second msg is always doing a loop wen including the first. Can't find anything to stop the split in the right way, as I can't base my split on counting => number of strings can be variable, so length also...

1st msg : IWAP02,zh_cn,0,7,460,0,9520|3671|13,9520|3672|12,9520|3673|11,9520|3674|10,9520|3675|9,9520|3676|8,9520|3677|7#

Actually this right split & format result when executed alone is :

460,0,9520,3671
460,0,9520,3672
460,0,9520,3673
460,0,9520,3674
460,0,9520,3675
460,0,9520,3676
460,0,9520,3677

2nd msg :
IWAP02,zh_cn,1,7,460,0,9520|3671|13,9520|3672|12,9520|3673|11,9520|3674|10,9520|3675|9,9520|3676|8,9520|3677|7,4,1|D8-24-BD-79-FA-1F|59&2|3C-46-D8-6D-CE-01|81&3|0C-4C-39-1A-7C-65|69&4|70-A8-E3-5D-D7-C0|65#

Actually this wrong split & format result when executed including the first msg is doing :

460,0,9520,3671
460,0,9520,3672
460,0,9520,3673
460,0,9520,3674
460,0,9520,3675
460,0,9520,3676
460,0,9520,3677
460,0,4 <= here starts the loop... and I have 9 objects, as I should have only 7 (but I can't limit as it could come more)
460,0,1 <= end ...

Here are the nodes below to consider the whole problem:

[{"id":"a44aa7ab.117b58","type":"tab","label":"Test Split","disabled":false,"info":""},{"id":"8f59e0b6.23c3d","type":"inject","z":"a44aa7ab.117b58","name":"LBS","topic":"","payload":"IWAP02,zh_cn,0,7,460,0,9520|3671|13,9520|3672|12,9520|3673|11,9520|3674|10,9520|3675|9,9520|3676|8,9520|3677|7#","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":85,"y":54,"wires":[["e93212c2.9da56"]]},{"id":"728ddf4.709982","type":"inject","z":"a44aa7ab.117b58","name":"LBS+WIFI","topic":"","payload":"IWAP02,zh_cn,1,7,460,0,9520|3671|13,9520|3672|12,9520|3673|11,9520|3674|10,9520|3675|9,9520|3676|8,9520|3677|7,4,1|D8-24-BD-79-FA-1F|59&2|3C-46-D8-6D-CE-01|81&3|0C-4C-39-1A-7C-65|69&4|70-A8-E3-5D-D7-C0|65#","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":93,"y":120.20001411437988,"wires":[["e93212c2.9da56"]]},{"id":"e93212c2.9da56","type":"function","z":"a44aa7ab.117b58","name":"Get flag and other parts","func":"// flag = 0 \n// IWAP02,zh_cn,0,7,460,0,9520|3671|13,9520|3672|12,9520|3673|11,9520|3674|10,9520|3675|9,9520|3676|8,9520|3677|7#\n\n// flag = 1\n// IWAP02,zh_cn,1,7,460,0,9520|3671|13,9520|3672|12,9520|3673|11,9520|3674|10,9520|3675|9,9520|3676|8,9520|3677|7,4,1|D8-24-BD-79-FA-1F|59&2|3C-46-D8-6D-CE-01|81&3|0C-4C-39-1A-7C-65|69&4|70-A8-E3-5D-D7-C0|65#\n\nmsg.elements = msg.payload.replace('#','').split(\",\"); // this gives an array containing the bits\n\n// Get flag\nmsg.flag = msg.elements[2] // 0\n\n// Get baseCountgsmTower\nmsg.baseCountgsmTower = Number(msg.elements[3]) // 7\nmsg.mmc = msg.elements[4] // 226\nmsg.mnc = msg.elements[5] // 10\n\n// Get baseCountwifiAccessPoints\nmsg.baseCountwifiAccessPoints = Number(msg.elements[13]) // 4\n\nreturn msg","outputs":1,"noerr":0,"x":333.00000762939453,"y":80.20001220703125,"wires":[["58eaf0fd.8870b","c1a2eeff.031dd"]]},{"id":"59f87170.6b9a6","type":"function","z":"a44aa7ab.117b58","name":"Prepare msg type 0","func":"//Prepare JS object and fill out fields before conversion to XML\n\nvar LocationRQ = {\"$\":{\"xmlns\":\"http://skyhookwireless.com/wps/2005\",\"version\":\"2.26\",\"street-address-lookup\":\"full\"},\"authentication\":[{\"$\":{\"version\":\"2.2\"},\"key\":[{\"$\":{\"key\":\"XYXYXXYXYXYXYXYXYXYXXYXYXYXYYXYXYXXY\",\"username\":\"ABABABABABABABABAAB\"}}]}],\"gsm-tower\":[]}\n\n// flag = 0 \n// IWAP02,zh_cn,0,7,226,10,1440|3671|13,9520|3672|12,9520|3673|11,9520|3674|10,9520|3675|9,9520|3676|8,9520|3677|7#\n\nlet elements = msg.elements; \nLocationRQ.flag = msg.flag; // true or false - A or V\nLocationRQ.gsmTowerStrings = msg.gsmTowerStrings\n\n// Get LBS datas\nlet i;\nlet gsmTowerStrings = LocationRQ.gsmTowerStrings;\nlet j;\nlet gsmTower = [];\nfor (j = 6; j < msg.elements.length; j++) {\n let spl = msg.elements[j].split(\"|\")\n gsmTower.push({ mcc: msg.mmc, mnc: msg.mnc, lac: spl[0], ci: spl[1] }) // 460,0,9520|3671|\n}\n// adjust baseCountgsmTower just in case the number actually there didn't match that expexted\nLocationRQ.baseCountgsmTower = gsmTower.length\nLocationRQ[\"gsm-tower\"] = gsmTower;\n\nmsg.payload = {LocationRQ: LocationRQ};\nreturn msg\n\n\n\n\n\n\n\n\n","outputs":1,"noerr":0,"x":558,"y":177.20001316070557,"wires":[["904565e.bfb9798","e738a0af.fdfe4"]]},{"id":"e738a0af.fdfe4","type":"function","z":"a44aa7ab.117b58","name":"Format msg 0","func":"delete msg.flag;\ndelete msg.elements;\ndelete msg.baseCountgsmTower;\ndelete msg.mmc;\ndelete msg.mnc;\ndelete msg.baseCountwifiAccessPoints;\ndelete msg.payload.LocationRQ.flag;\ndelete msg.payload.LocationRQ.gsmTowerStrings;\ndelete msg.payload.LocationRQ.baseCountgsmTower;\n\n\nreturn msg;","outputs":1,"noerr":0,"x":546,"y":209.20001411437988,"wires":[["67257b91.944644"]]},{"id":"c1a2eeff.031dd","type":"switch","z":"a44aa7ab.117b58","name":"Route 0/1","property":"flag","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"str"},{"t":"eq","v":"1","vt":"str"}],"checkall":"true","repair":true,"outputs":2,"x":230,"y":237.20001602172852,"wires":[["59f87170.6b9a6"],["13a76d16.cad7d3"]]},{"id":"58eaf0fd.8870b","type":"debug","z":"a44aa7ab.117b58","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":271.00001525878906,"y":112.20001411437988,"wires":[]},{"id":"13a76d16.cad7d3","type":"function","z":"a44aa7ab.117b58","name":"Prepare msg type 1","func":"//Prepare JS object and fill out fields before conversion to XML\n\nvar LocationRQ = {\"$\":{\"xmlns\":\"http://skyhookwireless.com/wps/2005\",\"version\":\"2.26\",\"street-address-lookup\":\"full\"},\"authentication\":[{\"$\":{\"version\":\"2.2\"},\"key\":[{\"$\":{\"key\":\"XYXYXXYXYXYXYXYXYXYXXYXYXYXYYXYXYXXY\",\"username\":\"ABABABABABABABABAAB\"}}]}],\"access-point\":[],\"gsm-tower\":[]}\n\n\n// flag = 1\n// IWAP02,zh_cn,1,7,460,0,9520|3671|13,9520|3672|12,9520|3673|11,9520|3674|10,9520|3675|9,9520|3676|8,9520|3677|7,    4,1|D8-24-BD-79-FA-1F|59&2|3C-46-D8-6D-CE-01|81&3|0C-4C-39-1A-7C-65|69&4|70-A8-E3-5D-D7-C0|65#\n\nlet elements = msg.elements; \nLocationRQ.flag = msg.flag; // 0 or 1\nLocationRQ.gsmTowerStrings = msg.gsmTowerStrings\n\n// Get LBS datas\nlet i;\nlet gsmTowerStrings = LocationRQ.gsmTowerStrings;\nlet j;\nlet gsmTower = [];\nfor (j = 6; j < msg.elements.length; j++) {\n let spl = msg.elements[j].split(\"|\")\n gsmTower.push({ mcc: msg.mmc, mnc: msg.mnc, lac: spl[0], ci: spl[1] }) // 460,0,9520|3671|\n}\n// adjust baseCountgsmTower just in case the number actually there didn't match that expexted\nLocationRQ.baseCountgsmTower = gsmTower.length\nLocationRQ[\"gsm-tower\"] = gsmTower;\n\n// Get WIFI datas \nlet k;\nlet wifiAccessPointsStrings = elements[14].split(\"&\");\nlet l;\nlet ap = [];\nfor (l = 0; l < wifiAccessPointsStrings.length; l++) {\n let baseElements = wifiAccessPointsStrings[l].split(\"|\")\n ap.push({ mac: baseElements[1].replace(/:/g,\"\"), ssid: baseElements[0], [\"signal-strength\"]: baseElements[2] }) \n}\n// adjust baseCountwifiAccessPoints just in case the number actually there didn't match that expexted\nLocationRQ.baseCountwifiAccessPoints = ap.length\nLocationRQ[\"access-point\"] = ap;\n\n\nmsg.payload = {LocationRQ: LocationRQ};\nreturn msg\n\n\n\n\n\n\n\n\n","outputs":1,"noerr":0,"x":556,"y":285.2000141143799,"wires":[["81bcd4ed.43f208","e479155e.84e108"]]},{"id":"e479155e.84e108","type":"function","z":"a44aa7ab.117b58","name":"Format msg 1","func":"\ndelete msg.flag;\ndelete msg.elements;\ndelete msg.baseCountgsmTower;\ndelete msg.mmc;\ndelete msg.mnc;\ndelete msg.baseCountwifiAccessPoints;\ndelete msg.payload.LocationRQ.flag;\ndelete msg.payload.LocationRQ.gsmTowerStrings;\ndelete msg.payload.LocationRQ.baseCountgsmTower;\ndelete msg.payload.LocationRQ.baseCountwifiAccessPoints;\nvar msg1={};\n\nmsg1.payload=msg.payload;\nreturn msg1;","outputs":1,"noerr":0,"x":545,"y":317.2000102996826,"wires":[["a65ccf87.8c1d1"]]},{"id":"81bcd4ed.43f208","type":"debug","z":"a44aa7ab.117b58","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":788.0000114440918,"y":315.20001316070557,"wires":[]},{"id":"a65ccf87.8c1d1","type":"debug","z":"a44aa7ab.117b58","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":788.0000114440918,"y":355.2000141143799,"wires":[]},{"id":"904565e.bfb9798","type":"debug","z":"a44aa7ab.117b58","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":794.0000114440918,"y":155.20001316070557,"wires":[]},{"id":"67257b91.944644","type":"debug","z":"a44aa7ab.117b58","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":795.0000114440918,"y":196.20001316070557,"wires":[]},{"id":"e27fa2d2.cb2a1","type":"comment","z":"a44aa7ab.117b58","name":"SPLIT AND FORMAT OK","info":"","x":572.2999877929688,"y":144.8000030517578,"wires":[]},{"id":"1cc4508d.135d9f","type":"comment","z":"a44aa7ab.117b58","name":"LOOP SPLIT AND FORMAT ISSUE","info":"","x":601.0000076293945,"y":251.00000190734863,"wires":[]}]

Thanks for your help.
jycemea

What is the exact format of the string? Do you have a link to the specification? That would help someone help you

Isn't one of the items the count?

I.e. is that 7 not telling you there are 7 items? IWAP02,zh_cn,1, 7,. If so, then drop out of the for loop after 7 items collected.

Alternatively, when you loop through the items, stop when the split by | doesn't contain 3 items.

Thanks for your answer, Steve.

What is the exact format of the string? Do you have a link to the specification? That would help someone help you

Here are the specifications
IWAP02-1

Isn't one of the items the count?
yes of course, in the 1st and 2nd msg 7 is the count for gsmTower and in the 2nd msg 4 is the count for ap

IWAP02,zh_cn,1,7,460,0,9520|3671|13,9520|3672|12,9520|3673|11,9520|3674|10,9520|3675|9,9520|3676|8,9520|3677|7 ,4,1|D8-24-BD-79-FA-1F|59&2|3C-46-D8-6D-CE-01|81&3|0C-4C-39-1A-7C-65|69&4|70-A8-E3-5D-D7-C0|65#

I.e. is that 7 not telling you there are 7 items? IWAP02,zh_cn,1, 7, . If so, then drop out of the for loop after 7 items collected.

If I knew how to do this, I would not ask...

^ That is the answer ^

Clue ...

//Get the count of towers
var gsmTowerCount = data[4];
for(X=6;X<array.length; X++){
   //Existing code adding to towers array
    //When the towers array is complete, break out of the loop
    if(towers.length >= gsmTowerCount)
        break; //drop out of the loop
}

Note, this is pseudo code (untested) it is an example to help you only.

I told you how to do this previously. You don't need to use the flag to tell you what is coming. The two messages are the same except that the second one has extra data. When you get to the end of the first array of seven you just need to check if there is more data, if there is then it is the data as in the second message. As for the actual code then if you have not by now picked up enough javascript to allow you to make an attempt at it then you need to take a few days out and work right through a good js tutorial rather than continually expecting others to write all your code for you.

Hi Colin, the code you wrote was only operational for 1 provider... but not in XML and not for this service provider.
As sometimes, I need to use either/and provider 1..or provider 2!! It depends about the precision of the geolocalization info returned.
Do you seriously think learning js is my only problem here??? I've spent 10h/day/2 months (not talking about my job) and I still need 3 - 4 months like this.

Of course you're right, I should learn js for 1 year before doing anything... but I understood Node-Red was created to simplify the job!!! :slight_smile:

Anyway, I decided to trust in Node-Red...

Thanks Steve! I'm going to experiment and to see if I need to repeat the code for wifiaccesspoint, also...