Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

DOWNLOADS: access to product manuals, support files and drivers

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

NEW REGISTRATIONS - please contact us if you wish to register on the forum

Adding a string to string array's indexes with ruby; how?

For general discussion related FlowStone

Re: Adding a string to string array's indexes with ruby; how

Postby kortezzzz » Wed Mar 11, 2020 7:54 pm

RJHollins wrote:Let me ask ... do you just enclose the whole thing into a new module, and then put that into the toolbox ?


Yes :D of course, I first name the enclosed module with my own key words, so whenever I need it, I can't do any mistakes. I'll find it. This may sound as a very primitive and non-elegant solution, but it works for me 8-)
User avatar
kortezzzz
 
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Re: Adding a string to string array's indexes with ruby; how

Postby pshannon » Wed Mar 11, 2020 9:48 pm

kortezzzz wrote:
RJHollins wrote:Let me ask ... do you just enclose the whole thing into a new module, and then put that into the toolbox ?


Yes :D of course, I first name the enclosed module with my own key words, so whenever I need it, I can't do any mistakes. I'll find it. This may sound as a very primitive and non-elegant solution, but it works for me 8-)


Everyone's code earlier was great, but here is a simple module. I hope it helps.

Open the module and a help file plus code is in there with a reference. Ruby is not my first language of choice, however, it is what we have for FS and it brings designs to the next level.
Attachments
simple ruby code for strings_patrick.fsm
(3.31 KiB) Downloaded 799 times
User avatar
pshannon
 
Posts: 144
Joined: Fri Jan 02, 2015 3:08 am

Re: Adding a string to string array's indexes with ruby; how

Postby kortezzzz » Wed Mar 11, 2020 10:06 pm

pshannon wrote:Everyone's code earlier was great, but here is a simple module. I hope it helps.

Open the module and a help file plus code is in there with a reference. Ruby is not my first language of choice, however, it is what we have for FS and it brings designs to the next level.


Beautiful, Patrick! :) Thank you so much for this tool (and the dedicated GUI!). I know that most of the "silent crowd" on this forum which hardly write comments are Desperately waiting for such tools, since they chose FS because of it's ability to work with graphical elements. Those pieces of codes aren't very different from greens and blues, and that's why it's so important to share them here. Thank you.
User avatar
kortezzzz
 
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Re: Adding a string to string array's indexes with ruby; how

Postby tulamide » Wed Mar 11, 2020 10:56 pm

pshannon wrote:Everyone's code earlier was great, but here is a simple module. I hope it helps.

Open the module and a help file plus code is in there with a reference. Ruby is not my first language of choice, however, it is what we have for FS and it brings designs to the next level.

I love your attitude! You surely don't lack self-confidence, Mr Coolhacker :mrgreen:

I hope you don't mind, but there was a reason I used
Code: Select all
"text" << "newtext"


and not
Code: Select all
"text" + "newtext"


It's a little trick to save some time. It adds up the longer the array is, so it's worth it. text + newtext tells Ruby to create a new string class with the combined text and newtext.
Whereas text << newtext tells Ruby to use the already existing class and just append newtext. That saves you the time and memory needed to create a class instance.

If you would change your module to do that as well, it will be a good tool for the toolbox.
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2687
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Adding a string to string array's indexes with ruby; how

Postby pshannon » Thu Mar 12, 2020 2:02 am

tulamide wrote:
pshannon wrote:Everyone's code earlier was great, but here is a simple module. I hope it helps.

Open the module and a help file plus code is in there with a reference. Ruby is not my first language of choice, however, it is what we have for FS and it brings designs to the next level.

I love your attitude! You surely don't lack self-confidence, Mr Coolhacker :mrgreen:

I hope you don't mind, but there was a reason I used
Code: Select all
"text" << "newtext"


and not
Code: Select all
"text" + "newtext"


It's a little trick to save some time. It adds up the longer the array is, so it's worth it. text + newtext tells Ruby to create a new string class with the combined text and newtext.
Whereas text << newtext tells Ruby to use the already existing class and just append newtext. That saves you the time and memory needed to create a class instance.

If you would change your module to do that as well, it will be a good tool for the toolbox.


Wow, I got the attention from the popular tula. I have read many of your posts and downloaded several examples from you. You know your code and FS very well. Thanks for the tip and I didn't know it made a difference in the ruby interpreter. I work in cyber security for the past 16 years and someone once called me "Coolhacker" and it stuck. :) Ruby is the newest language for me vs previous traditional ones. It looked like someone really needed help with this. I did try your suggestion for optimized code which is always welcomed. For some reason it kept adding more strings and produced unpredictable results. It did work well with the static arrays like you demonstrated, I wonder if the green string array outputs something different? I uploaded one more revision with some more added features. If someone wants to try the << vs the +, it works for me.
Attachments
simple ruby code for strings 1.1_patrick.fsm
(29.66 KiB) Downloaded 786 times
User avatar
pshannon
 
Posts: 144
Joined: Fri Jan 02, 2015 3:08 am

Re: Adding a string to string array's indexes with ruby; how

Postby RJHollins » Thu Mar 12, 2020 3:27 am

Nice update ..... Thanks !

I could not get the << to work either ... it was adding 'more strings' as you say.
RJHollins
 
Posts: 1568
Joined: Thu Mar 08, 2012 7:58 pm

Re: Adding a string to string array's indexes with ruby; how

Postby tulamide » Thu Mar 12, 2020 10:20 am

pshannon wrote:I work in cyber security for the past 16 years and someone once called me "Coolhacker" and it stuck. :)
I like that. You don't get such a nickname by accident!

pshannon wrote:It looked like someone really needed help with this.
We could use a lot more of your kind on the Ruby front. It seems lately there's only trog and I left to help. It can become a little too much at times. So, any other helping hand is welcome.

pshannon wrote:I did try your suggestion for optimized code which is always welcomed. For some reason it kept adding more strings and produced unpredictable results. It did work well with the static arrays like you demonstrated, I wonder if the green string array outputs something different?
I didn't spend enough time on your code, and that's why my tip didn't work as you would wish.

Remember, "<<" appends the right class to the existing left class, without creating a new class. Since you use the inputs directly, you change the content of those, in this case @stringsub.

::map iterates over the original stringarray, which is 14 items. Everytime the "<<"-method appends to the original @stringsub.
1st iter. @stringsub becomes "red bike"
2nd iter. @stringsub becomes "red bikecar"
3rd iter. @stringsub becomes "red bikecarplane"
etc.
But @stringsub also replaces the item in the array (that's what ::map does), and so it ends up with a mess of predictable but very wrong results. Furthermore, @stringsub isn't cleared for the next time, so the already long @stringsub again goes through the iteration, becoming an even longer nonsense.

This could even crash Flowstone at one point, when RAM isn't sufficient or the timeout is reached.

That was the long explanation.

Here's the short: I derped.

In your tool, a new empty string class is needed per iteration, which is most simply done by using +. (There are other ways as well, but why inventing the wheel a second time?)

I'm sorry for any confusion my tip may have caused!
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2687
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Adding a string to string array's indexes with ruby; how

Postby pshannon » Thu Mar 12, 2020 9:39 pm

You didn't cause any issues for me and I took it as a teaching moment from you. I get what you meant it was predictable, but an unwanted outcome and it was bad wording on my part. I could see the patterns and thanks for the explanations.
User avatar
pshannon
 
Posts: 144
Joined: Fri Jan 02, 2015 3:08 am

Re: Adding a string to string array's indexes with ruby; how

Postby RJHollins » Fri Mar 13, 2020 5:11 am

Thanks for the explanation tulamide.
I try to follow along [to learn] as best I can.

8-)
RJHollins
 
Posts: 1568
Joined: Thu Mar 08, 2012 7:58 pm

Previous

Return to General

Who is online

Users browsing this forum: No registered users and 23 guests