Page 1 of 1

something quick, to convert text to string and back

Posted: Sat Oct 26, 2013 8:24 pm
by tester
I just noticed, that green prims based procedure for converting multiline text to single-line string and back again, using standard green prims - is very slow (i.e. sluggish refresh).

But by converting text to string, I mean - changing all commas into something else (because comma sign interferes with arrays philosophy in FS), and then the end of lines into second user defined character. It's for texts longer than 255 chars in terms of single string, but that's not a problem for most FS prims.

Like this:

a,v,b,n
q,w,e

into:

a^v^b^n\q^w^e

and then back again into multiline text like above.

So - how to quickly convert texts that way using ruby, with no big lags in refreshing? (and without having too many triggers outside too). I could resign from using comma, but it's a matter of user's interface.

Re: something quick, to convert text to string and back

Posted: Sat Oct 26, 2013 8:37 pm
by KG_is_back
I think the fastest way would be to convert string into float array (simply by converting the format - bits untouched!). Convert the float array to mem - the mem readout to assembly block and from there to analyzer, form analyzer to Float array to string array converter (again, leaving the bit information untouched - only changing the format).
The assembly block would read the input and comparing it with bit pattern of comma and newline, if detected replace output with bit pattern of desired character.
However you'll have to find out how to change string into float array and back without affecting the actual stored information bitwise...

Re: something quick, to convert text to string and back

Posted: Sat Oct 26, 2013 8:51 pm
by tester
This is just a text within app; multiple blocks (dynamic amount) of viewable text (associated with some other data in the background). Rather ruby should do this job quickly, nothing else.

Re: something quick, to convert text to string and back

Posted: Sat Oct 26, 2013 10:25 pm
by Nubeat7
you can use the methode ".tr" from the string class, depending on what you are doing with it you can use the destructive or nondestructive one

http://ruby-doc.org/core-2.0.0/String.html#method-i-tr

like

Code: Select all

@in.tr!(",","^") 

Re: something quick, to convert text to string and back

Posted: Sat Oct 26, 2013 10:47 pm
by tester
Which one will be faster?

What is the practical difference between destructive and non-destructive? A some sort of passthrough like additional "dummy" connectors in FS?

//edit: works definately faster than green prims, on the fly, thanks! :-)
BTW, and how to create multiple conversions within one code? Just copy lines changing chars to change, or there is a faster way?

Re: something quick, to convert text to string and back

Posted: Sat Oct 26, 2013 10:56 pm
by tester
Okay, I have a small problem.

It returns output only if input has commas, but empty, when it's without comma.

Re: something quick, to convert text to string and back

Posted: Sat Oct 26, 2013 11:32 pm
by Nubeat7
hmm interesting, i dont know why but use it without "!" then it works right!

the differnce between destructive and nondestructive is that the original element stays original and a modified copy is returned when using a nondestructive (without "!") methode while when using the destructive version the original element gets modified like

a = 1,2,3,4

output 0, a.tr (",","^") => 1^2^3^4 returns the modified copy
output 1, a => 1,2,3,4 a by itself is not touched

while using destructive version

output 0, a.tr! (",","^") => 1^2^3^4 returns the modified original
output 1, a => 1^2^3^4 the original is modified now!


for multiple changes you can take the same methode, just say what you want to change:

q=aaabbbcccddd
q.tr("a,b","x,y") => xxxyyycccddd

Re: something quick, to convert text to string and back

Posted: Sat Oct 26, 2013 11:40 pm
by tester
Yes, I just figured out, that without "!" works for some reason. :-)
Maybe this is something like "return nothing if nothing changed"?

For me, the difference between destructive and non-destructive looks like simple additional "passthrough". I guess this is because it's more useful in different environments, that don't work like FS. Destructive shoud then be faster, and less memory usage (no copy of input) then.

Okay, and how to combine multiple character changes (in single text; like comma and new line) in one ruby code? I mean without cascading the module in FS.

//edit: you were faster. :-)

Re: something quick, to convert text to string and back

Posted: Sat Oct 26, 2013 11:47 pm
by Nubeat7
tester wrote:Yes, I just figured out, that without "!" works for some reason. :-)
Maybe this is something like "return nothing if nothing changed"?

For me, the difference between destructive and non-destructive looks like simple additional "passthrough". I guess this is because it's more useful in different environments, that don't work like FS. Destructive shoud then be faster, and less memory usage (no copy of input) then.

Okay, and how to combine multiple character changes (in single text; like comma and new line) in one ruby code? I mean without cascading the module in FS.


just edited the last answer for multiple changes... you was too fast!


the destructive version dont work because it returns nil if there was nothing changed! (info: ruby doc)

Re: something quick, to convert text to string and back

Posted: Sat Oct 26, 2013 11:49 pm
by tester
Yes, I just noticed it. So it looks that the destructive method is like passthrough plus some sort of boolean indicator (result or nil).

Okay, thanks for help.