array to string and string to array?
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- Posts: 38
- Joined: Tue Sep 20, 2011 5:01 pm
- Contact:
array to string and string to array?
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.
Thanks.
Bill Prothero
Re: array to string and string to array?
Split & Combine
Simon
Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!
Re: array to string and string to array?
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
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
-
- VIP Livecode Opensource Backer
- Posts: 10044
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: array to string and string to array?
I've been using split and combine. What does your source data look like?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: array to string and string to array?
Hi Richard.
I am likely misunderstanding what either you or Simon are saying. In a button:
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:
And which can be generalized as required, if the argument string elements are delimited by commas or whatever.
Craig
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
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
Craig
Re: array to string and string to array?
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.
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?
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
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?
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?
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
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