Creating a text array

634 views
Skip to first unread message

David Dempster

unread,
Jul 1, 2017, 8:30:00 AM7/1/17
to Node-RED
Hi to the Node-Red Group

Is it possible to create a text array and save it in a global variable using global.set (  ) ?

Any help/advice would be appreciated.

Cheers from David

Mark Setrem

unread,
Jul 1, 2017, 8:57:16 AM7/1/17
to Node-RED
Yes

[{"id":"d7717e9.624828","type":"tab","label":"Flow 1"},{"id":"54b3d2ce.6a0bac","type":"inject","z":"d7717e9.624828","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":112.5,"y":173,"wires":[["8c734d56.a1a988"]]},{"id":"8c734d56.a1a988","type":"function","z":"d7717e9.624828","name":"","func":"var cars = [\"Saab\", \"Volvo\", \"BMW\"];\nglobal.set(\"foo\",cars);  // this is now available to other nodes\nreturn msg;","outputs":1,"noerr":0,"x":257.5,"y":174,"wires":[[]]},{"id":"2681bcbc.79074c","type":"inject","z":"d7717e9.624828","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"x":103.5,"y":230,"wires":[["c32e04ba.57274"]]},{"id":"c32e04ba.57274","type":"change","z":"d7717e9.624828","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"foo","tot":"global"}],"action":"","property":"","from":"","to":"","reg":false,"x":273.5,"y":229,"wires":[["e5865a82.625f2"]]},{"id":"e5865a82.625f2","type":"debug","z":"d7717e9.624828","name":"","active":true,"console":"false","complete":"false","x":459.5,"y":229,"wires":[]}]

David Dempster

unread,
Jul 1, 2017, 8:59:31 AM7/1/17
to Node-RED
Hi Mark
Thanks for your swift response - I'll give it a try.

Cheers from David

Mark Setrem

unread,
Jul 1, 2017, 9:05:51 AM7/1/17
to Node-RED

No problem in the example the first line creates an array and stores it in a global variable, the second line retrieves sets msg.payload to be the global variable contents

David Dempster

unread,
Jul 1, 2017, 9:08:45 AM7/1/17
to Node-RED
Hi Mark,
Just tried it - works a treat.

I was being so complicated with my syntax.

Once I saw your code it made perfect sense.

Thanks again for your help.

David

David Dempster

unread,
Jul 1, 2017, 2:16:22 PM7/1/17
to Node-RED
Hi Mark,
Do you know the answer to this?
I'm trying to extract a specific element of a text array.

If I use the numerical 2 (as below), I can get the third element of the text array, but if I use index (which is set to 2) I get an error message.

var index = 2;

var prefix = global.get("instance.2");
msg.payload = prefix;
return msg;

David Dempster

unread,
Jul 1, 2017, 2:21:44 PM7/1/17
to Node-RED
This syntax fails with "unexpected c at position 9.
Any suggestions what I'm doing wrong?

var count = 3;
var test = global.get("instance[count]");
msg.payload = test;
return msg;

Nick O'Leary

unread,
Jul 1, 2017, 2:43:17 PM7/1/17
to Node-RED

"instance[count]" is a literal string. The fact you have a variable called count doesn't have any bearing on that string.

You need to build the string:

var count = 3;
var test = global.get("instance["+count+"]");


msg.payload = test;
return msg;

Or, do it in two steps - get the compete array and then extract the individual element:

var count = 3;
var data = global.get("instance");
msg.payload = data[count];
return msg;

Nick


--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/bdc63b1f-6e37-42f4-b621-eae64c180e71%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Dempster

unread,
Jul 1, 2017, 2:49:38 PM7/1/17
to Node-RED
Thanks Nick for your swift response.

Cheers from David.
Reply all
Reply to author
Forward
0 new messages