Page 1 of 1
array to string and string to array?
Posted: Tue Oct 30, 2012 8:11 pm
by waprothero
I read in one of the tutorials where there was a command to convert a list to a string and then back from a string to a list. I can't find that command. Anybody know what it is?
Thanks.
Re: array to string and string to array?
Posted: Tue Oct 30, 2012 8:21 pm
by Simon
Split & Combine
Simon
Re: array to string and string to array?
Posted: Wed Oct 31, 2012 9:10 pm
by dunbarx
Simon.
At first glance, this seemed sound (and clever) even though these two commands are explicitly designed to work with arrays.
But on testing it does not work. I placed the following text into a field: "aa bb cc"
In a button I combined that text with "return" as the primary delimiter, thinking I might see:
aa
bb
cc
But no such luck. Neither form of "combine" (by, using) works that way, And "split" automatically creates an array, so it is not ever applicable. Of course, if that original string were the elements of an array, I would have gotten exactly that list.
So, Waprothero, there might be such a pair of native commands lurking somewhere in the language. After all, it contains almost everything imaginable. But I do not see them offhand. Of course, it is simple to write a function that does this, along with any delimiter that you care to use. Do you need help with that? Ask if you do, otherwise, please post those mysterious commands if you find them.
Craig Newman
Re: array to string and string to array?
Posted: Wed Oct 31, 2012 9:42 pm
by FourthWorld
I've been using split and combine. What does your source data look like?
Re: array to string and string to array?
Posted: Thu Nov 01, 2012 1:08 am
by dunbarx
Hi Richard.
I am likely misunderstanding what either you or Simon are saying. In a button:
Code: Select all
on mouseUp
--get "aa,bb,cc" --commas
get "aa bb cc" --spaces
combine it using return
--combine it by row
answer it
end mouseUp
Using any of the options in that hander, I get empty as the answer. Which is what I always thought I would get, until that intriguing post by Simon. So where can either "split" or "combine" be used on ordinary strings, as opposed to arrays?
This in lieu of something like:
Code: Select all
on mouseUp
answer stringToList("aa bb cc")
end mouseUp
function stringToList var
repeat for each word tWord in var
put tWord & return after temp
end repeat
delete the last char of temp
return temp
end stringToList
And which can be generalized as required, if the argument string elements are delimited by commas or whatever.
Craig
Re: array to string and string to array?
Posted: Thu Nov 01, 2012 1:47 am
by sturgis
To do the "aa bb cc" are designating what char to split at
So in this case
put "aa bb cc" into temp
split temp by space
which results in a 3 key array (1,2,3) with the values aa, bb, cc respectively
If you split by return, since its only 1 line it SHOULd place it into temp[1] where the value is "aa bb cc"
If there were multiple lines seperated by return each line would be placed into a numerically keyed array with 1 line to a key.
dunbarx wrote:Simon.
At first glance, this seemed sound (and clever) even though these two commands are explicitly designed to work with arrays.
But on testing it does not work. I placed the following text into a field: "aa bb cc"
In a button I combined that text with "return" as the primary delimiter, thinking I might see:
aa
bb
cc
But no such luck. Neither form of "combine" (by, using) works that way, And "split" automatically creates an array, so it is not ever applicable. Of course, if that original string were the elements of an array, I would have gotten exactly that list.
So, Waprothero, there might be such a pair of native commands lurking somewhere in the language. After all, it contains almost everything imaginable. But I do not see them offhand. Of course, it is simple to write a function that does this, along with any delimiter that you care to use. Do you need help with that? Ask if you do, otherwise, please post those mysterious commands if you find them.
Craig Newman
Re: array to string and string to array?
Posted: Thu Nov 01, 2012 3:42 am
by dunbarx
Sturgis.
The original poster (is that now a valid neologism?) wanted to find a command that worked entirely in normal strings. I think. No arrays were mentioned. He wanted a "stringToList tDelimiter" command (though not specifying the delimiter in the source). In other words, to do what a simple function would do. My short example assumes spaces in the source, and I assume returns in the list.
Making or deconstructing arrays is standard fare. I may be wrong, but I do not believe that arrays were intended at all.
Craig
Re: array to string and string to array?
Posted: Thu Nov 01, 2012 3:52 am
by sturgis
Ah k. Took the array stuff from the topic title, and then saw you post that didn't seem to make sense to me. (my slow noggin) so was just clarifying.
Re: array to string and string to array?
Posted: Thu Nov 01, 2012 4:11 am
by dunbarx
Hi.
Well, maybe I am wrong and the topic title is what he really meant, and just did not explain it well in the body.
In that case, Simon's one liner is all anyone ever needed to know.
So, Waprothero, what about all that?
Craig