array to string and string to array?

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
waprothero
Posts: 38
Joined: Tue Sep 20, 2011 5:01 pm
Contact:

array to string and string to array?

Post by waprothero » Tue Oct 30, 2012 8:11 pm

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.
Bill Prothero

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3901
Joined: Sat Mar 24, 2007 2:54 am

Re: array to string and string to array?

Post by Simon » Tue Oct 30, 2012 8:21 pm

Split & Combine

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10309
Joined: Wed May 06, 2009 2:28 pm

Re: array to string and string to array?

Post by dunbarx » Wed Oct 31, 2012 9:10 pm

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10044
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: array to string and string to array?

Post by FourthWorld » Wed Oct 31, 2012 9:42 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10309
Joined: Wed May 06, 2009 2:28 pm

Re: array to string and string to array?

Post by dunbarx » Thu Nov 01, 2012 1:08 am

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

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: array to string and string to array?

Post by sturgis » Thu Nov 01, 2012 1:47 am

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10309
Joined: Wed May 06, 2009 2:28 pm

Re: array to string and string to array?

Post by dunbarx » Thu Nov 01, 2012 3:42 am

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

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: array to string and string to array?

Post by sturgis » Thu Nov 01, 2012 3:52 am

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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10309
Joined: Wed May 06, 2009 2:28 pm

Re: array to string and string to array?

Post by dunbarx » Thu Nov 01, 2012 4:11 am

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

Post Reply