combine a multidimensional array [SOLVED]

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
atout66
Posts: 266
Joined: Wed Feb 02, 2011 12:31 pm

combine a multidimensional array [SOLVED]

Post by atout66 » Thu Apr 17, 2014 4:22 pm

Hi to all,

I've a problem when I want to combine an array as myArray[][]. I extract the array which is stored into a custom propertie of a field into an other var like explained in the LC's lesson.
http://lessons.runrev.com/s/lessons/m/4 ... able-field
Here is my code with comment:

Code: Select all

 --   From <leMot> we extract the custom propertie which is an array
   put the leMot of fld laCard of stack laStack into tArray   -- OK until here
   combine tArray using return -- destroy tArray !!!
   set the unicodeText of fld leFld to uniEncode (tArray, "UTF8") -- empty 
Any idea would be much appreciated :wink:

Kind regards, Jean-Paul.
Last edited by atout66 on Fri Apr 18, 2014 2:09 pm, edited 3 times in total.
Discovering LiveCode Community 6.5.2.

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: combine a multidimensional array

Post by Thierry » Thu Apr 17, 2014 5:45 pm

atout66 wrote: I've a problem when I want to combine an array as myArray[][].
AFAIK, split and combine doesn't work with multidimensional arrays.

You have to roll your own...

Regards,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: combine a multidimensional array

Post by magice » Thu Apr 17, 2014 6:38 pm

You need to use a repeat loop to combine the first dimension, than you can combine the second. This works well if the keys are numbers. One thing to be aware of though, is that if the second dimension contains more than 9 elements, the process can corrupt the order. This is because a computer will put 10 after 1 and before 2. Just keep that in mind when you split the array later.

Code: Select all

   
 repeat with i= 1 to tCounter --A variable i keep for the number of sets of data
    
      do "combine tArray["&i&"] using comma"
   end repeat
   
   combine tArray using cr

atout66
Posts: 266
Joined: Wed Feb 02, 2011 12:31 pm

Re: combine a multidimensional array

Post by atout66 » Thu Apr 17, 2014 7:53 pm

Thanks to all :wink:
You're right, I must use a loop. That's what I did like:

Code: Select all

get the keys of tArray -- = dim()
repeat with aa = 1 to the number of lines of IT -- read each element of the second column
      put tArray[1][aa] into laValeur
       set the unicodeText of fld leFld to uniEncode (laValeur, "UTF8") 
      wait 3 second -- to see
   end repeat
I'll look farther how to get a loop with the first column, here [1].

Kind regards, Jean-Paul.
Discovering LiveCode Community 6.5.2.

rkriesel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 119
Joined: Thu Apr 13, 2006 6:25 pm

Re: combine a multidimensional array [SOLVED]

Post by rkriesel » Thu Apr 17, 2014 9:19 pm

atout66 wrote:Any idea would be much appreciated :wink:
Hi, Jean-Paul. As you've seen, combining a two-dimensional array is fairly simple. Do you want to combine an n-dimensional array, as your subject line suggests?

-- Dick

atout66
Posts: 266
Joined: Wed Feb 02, 2011 12:31 pm

Re: combine a multidimensional array

Post by atout66 » Fri Apr 18, 2014 9:47 am

Hi Dick,
Yes, that's what I'm looking for.
Currently, I can read only one element of the first row, column after column.
But what if I have this kind of array ?
[1][1] , [1][2] , [1][3] , [1][4]
[2][1]
[3][1] , [3][2]
[4][1] , [4][2] , [4][3]
My script work for [1][1] , [1][2] , [1][3] , [1][4] ; OK 8)
But how can I guess that there is no [2][2] in that array ?
And how can I be sure that I must loop 4 columns in each row to be sure not to miss [1][4] value ?

Not simple at all for my current skill. With my old TBK (Toolbook) I could do that, but I'm discovering LC and it takes time and effort :wink:

Kind regards, Jean-Paul.
Discovering LiveCode Community 6.5.2.

rkriesel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 119
Joined: Thu Apr 13, 2006 6:25 pm

Re: combine a multidimensional array [SOLVED]

Post by rkriesel » Fri Apr 18, 2014 10:34 am

atout66 wrote:But what if I have this kind of array ?
[1][1] , [1][2] , [1][3] , [1][4]
[2][1]
[3][1] , [3][2]
[4][1] , [4][2] , [4][3]
Since your array has only two dimensions, you can combine it like this:

Code: Select all

repeat for each key tKey1 in tArray
    repeat for each key tKey2 in tArray[ tKey1 ]
        put tKey1, tKey2, tArray[ tKey1 ][ tKey2 ] & cr after tString
    end repeat
end repeat
To reconstitute your array, you can split the string like this:

Code: Select all

repeat for each line tLine in tString
    put item 3 of tLine into tArray[ item 1 of tLine ][ item 2 of tLine ]
end repeat
Note that the correctness of the code above depends on there being no comma or cr in your array.

Does that help?

-- Dick

atout66
Posts: 266
Joined: Wed Feb 02, 2011 12:31 pm

Re: combine a multidimensional array

Post by atout66 » Fri Apr 18, 2014 10:56 am

Hi Dick,
Does that help?
It could be :wink:
I've to look in deep at your code, specialy your first loop. I'm not used with the <for each> repeat.
Thanks for your help.

Kind regards, Jean-Paul.
Discovering LiveCode Community 6.5.2.

atout66
Posts: 266
Joined: Wed Feb 02, 2011 12:31 pm

Re: combine a multidimensional array

Post by atout66 » Fri Apr 18, 2014 2:08 pm

Ah ! I've got it Dick :wink:
Step by step inside the debugger, I understood your loop in the loop. Very handy, thanks a lot for your help.

Kind regards, Jean-Paul.
Discovering LiveCode Community 6.5.2.

Post Reply