# Pulling apart message arrays

**URL:** <https://discourse.nodered.org/t/pulling-apart-message-arrays/16041>\
**Category:** General\
**Created:** [28 September 2019 23:55 UTC](https://discourse.nodered.org/t/pulling-apart-message-arrays/16041 "2019-09-28T23:55:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [28 September 2019 23:55 UTC](https://discourse.nodered.org/t/pulling-apart-message-arrays/16041/1 "2019-09-28T23:55:23Z")

</div>

Ok, I'm stuck.

Messing around with a message which looks like:  
`n,r,g,b`  
(String but the values are numbers)

`n` ranges from 0 to 7.  
the others from 0 to 256.  
(Guess what I am doing - rhetorical)

Because I am curious I want to _mix and match_ the messages.

So though #0 is usually _on_ I want to blink it on/off.  
But that is difficult because I can't control message timing.

The idea is to store them as `flow` variables (or context, I guess) and when a message arrives the values are stored.  
When another message arrives, the new values are _added_ to the values.

Another part (so I guess the do need to be `flow`) these values are read in and sent out to the LEDs.

Part of the problem is how to merge them so I can both turn things on and off with ease.  
(I'm thinking XOR now)  
But the other part is how to pull apart the messages and store them.

I get that the first one is the _index_ but then I am stuck.

Basically this is the _idea_:  
I send `0,0,128,0`  
LED 0 turns on GREEN.  
I send `0,0,128,0` and LED 0 turns OFF. (Given no other signals in the mean time.)

That's nice all things being as they are, but it is difficult to also have a "TURN ALL LEDS OFF" function in there too..... Ok. Just realised: Set the values directly and not via the XOR.

But, back to the _problem_:  
How do I handle the message to set the `flow` values?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [29 September 2019 00:10 UTC](https://discourse.nodered.org/t/pulling-apart-message-arrays/16041/2 "2019-09-29T00:10:38Z")

</div>

> [@Trying\_to\_learn](#):
>
> But the other part is how to pull apart the messages and store them.

Are you generating this `n,r,g,b` message? Is that a string like it appears to be ? If so, why not generate and send an actual object with all the properties already separated out? That way there is no real requirement to "pull them apart"

> [@Trying\_to\_learn](#):
>
> How do I handle the message to set the `flow` values

This is a bit unclear? I'm sure you know a change node or function node can store values in context / flow. What is you real issue?

As I'm not inside your head and don't fully understand your requirements, I can only offer advice & that advice would be, generate objects that are relevant to your requirements and if you need to store values , then store the whole object and retrieve it as a while object.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [29 September 2019 00:17 UTC](https://discourse.nodered.org/t/pulling-apart-message-arrays/16041/3 "2019-09-29T00:17:41Z")

</div>

> [@Steve-Mcl](#):
>
> Are you generating this `n,r,g,b` message? Is that a string like it appears to be ?

Well, because - say I want LED 0 to be green.  
That is 0,0,255,0 Full green.

How else would I go about doing that?

In the `debug` node it shows up as `"0,0,255,0"`

That is done on/in another flow - else where.  
Rather than have `global` names.

So I am trying to confine the names to `flow`.

I need to send the _values_ to the flow which handles the LEDs.  
As `n,r,g,b` seems to be how the message is sent to the LEDs, why not use that method/structure?

(Sorry. Brain's a bit awash with other things as well.)

So the message arrives with the `n,r,g,b` and I want to _pull it apart_ to save the RGB values for each LED.

I don't think I could _merge_ the totalistic (yeah...) thing where I _merge_ the entire message.  
the first value is the LED, so it has to remain unique.

So I save the 8 RGB values as `flow` then in another part of the flow I read them in and send them to the LEDs.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [29 September 2019 00:21 UTC](https://discourse.nodered.org/t/pulling-apart-message-arrays/16041/4 "2019-09-29T00:21:50Z")

</div>

I literally have no idea what you're saying apart from ...

> [@Trying\_to\_learn](#):
>
> So the message arrives with the `n,r,g,b` and I want to _pull it apart_ to save the RGB values for each LED.

So my suggestion was why pull it apart if you're generating the msg yourself. You already know you can pass more than just `msg.payload` so why not send the payload string AND the component parts as `msg.r` and `msg.g` etc. Then from the very origin, they are already split up? Or am I missing something?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [29 September 2019 00:23 UTC](https://discourse.nodered.org/t/pulling-apart-message-arrays/16041/5 "2019-09-29T00:23:15Z")

</div>

No you aren't missing something.

I just hadn't thought of

> [@Steve-Mcl](#):
>
> so why not send the payload string AND the component parts as `msg.r` and `msg.g` etc. Then from the very origin, they are already split up?

Just didn't occur to me.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [29 September 2019 00:35 UTC](https://discourse.nodered.org/t/pulling-apart-message-arrays/16041/6 "2019-09-29T00:35:22Z")

</div>

Hope I helped in some way. Good luck.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [29 September 2019 00:37 UTC](https://discourse.nodered.org/t/pulling-apart-message-arrays/16041/7 "2019-09-29T00:37:18Z")

</div>

Me too. 🙂

---

<div class="post-metadata">

**Author:** ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)\
**Post date:** [29 September 2019 09:50 UTC](https://discourse.nodered.org/t/pulling-apart-message-arrays/16041/8 "2019-09-29T09:50:37Z")

</div>

Perhaps you like to split the string into the components?  
There ist a handy javascript method for this `array=string.split(',');`

```auto
var components = msg.payload.split(',');
msg.colors=global.get("colors") || {};
if (components.length>3) {
  var n=Number(components[0]);
  msg.colors[n]={};
  msg.colors[n].r=Number(components[1]);
  msg.colors[n].g=Number(components[2]);
  msg.colors[n].b=Number(components[3]);
}
global.set("colors",msg.colors);
return msg;

```

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/76fdf547876d88d9b22da2fb7ec46bfefdbb9a4e.png)

Here is the output:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/1/18995ce6eaaa7e2b5e83dd0558a629f8f5e69d1f.png)

This should give you an object with all your colors in the global store.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/7/755f6b5e7be01b5742a091113cb717e146bad63d.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [29 September 2019 10:05 UTC](https://discourse.nodered.org/t/pulling-apart-message-arrays/16041/9 "2019-09-29T10:05:26Z")

</div>

@Christian-Me sure there are plenty of ways of parsing a string.

I decided to centre my reply from a "take a step back" perspective (i.e. consider your design point of view).

For example, if you are the person generating a `msg` you can/should consider how it will be used. I.E... instead of sending `msg.payload="0,10,20,30"` you can just as easily send `msg.payload={n:0, r:10, g:20, b:30}"` then all this string splitting becomes completely unnecessary.

That said, if the `msg` is being generated by something out of your control & you have no means of formatting the src msg, your solution is perfectly fine.

---

<div class="post-metadata">

**Author:** ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)\
**Post date:** [29 September 2019 10:23 UTC](https://discourse.nodered.org/t/pulling-apart-message-arrays/16041/10 "2019-09-29T10:23:58Z")

</div>

I totally get your point. But sometimes there is little you can do about in which format you receive your data. For example: I do not agree on the color piker string format, but I see the use of it. Yes I can use the output as object but the fact that it is there says a lot. It is human readable and says everything about the value types and units. But to make it software readable you have to split it apart with a lot of effort.

That`s why I'm working on standardizing all my communication (especially over MQTT). Sometimes a standard is not the best I can imagine but a well documented open standard is better than nothing.
