merge two arrays

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
kimberlyn
Posts: 18
Joined: Mon Jul 01, 2013 1:40 pm

merge two arrays

Post by kimberlyn » Thu Jul 04, 2013 11:47 pm

Hi, I have to arrays containing strings tab1 and tab2.

I want to put the content of tab1 into tab2 but I have an error i can't fix :S !

Mu code is

Code: Select all

repeat with i = 1 to the number of lines in the keys of tab1
     put tab1[i] after tab2
end repeat
I also tried

Code: Select all

put the keys of tab1 into theKeys
   repeat for each item aLine in theKeys
   put aLine into tab2
end repeat 

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: merge two arrays

Post by bn » Fri Jul 05, 2013 12:10 am

kimberlyn,

if you want to duplicate the array tab1 with all its content you could just say

put tab1 into tab2

tab2 is contains a copy of tab1, both are now arrays with the same content and the same keys. In case tab2 was not used before in the handler it would be created and be an array thereafter because you put an array into it.

if you want to copy content from array tab1 to array tab2 then you could do

Code: Select all

repeat with i = 1 to the number of lines in the keys of tab1
     put tab1[i] into tab2[i]
end repeat
this replaces whatever content there was in tab2

if you want to append the content of array tab1 to the content of array tab2 then you could do

Code: Select all

repeat with i = 1 to the number of lines in the keys of tab1
     put tab1[i] after tab2[i]
end repeat
this appends the content of tab1 to the content of tab2

If this is not what you are looking for you would have to explain in more detail what exactly it is that you want.

you also might want to have a look at the lessons, e.g.
http://lessons.runrev.com/s/lessons/sea ... mit=Search
Kind regards
Bernd

kimberlyn
Posts: 18
Joined: Mon Jul 01, 2013 1:40 pm

Re: merge two arrays

Post by kimberlyn » Fri Jul 05, 2013 1:38 am

Well I just want to add the elements of tab1 into tab2

for example :

Code: Select all

   put "a" & cr after tab1
   put "b" & cr after tab1   
   put "c" & cr after tab1
   
   put "d" & cr after tab2
   put "e" & cr after tab2   
   put "f" & cr after tab2

split tab1 with cr
split tab2 with cr --It's not the best way but in my case that's what I have
and I want at the end when I print tab2

Code: Select all

a
b
c
d
e
f
appending gives me

Code: Select all

da
eb
fc

kimberlyn
Posts: 18
Joined: Mon Jul 01, 2013 1:40 pm

Re: merge two arrays

Post by kimberlyn » Fri Jul 05, 2013 1:42 am

I found this way :

Code: Select all

put the number of elements of tab2 into var
  repeat with i = 1 to the number of lines in the keys of tab1
     put tab1[i] into tab2[i+var]
  end repeat

snm
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 253
Joined: Fri Dec 09, 2011 11:17 am

Re: merge two arrays

Post by snm » Fri Jul 05, 2013 5:42 am

The problem start if you have kes as string of characters, there are the same keys in both arrays and their values are different. You must decide what to do in such case.

Marek

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 332
Joined: Sun Apr 15, 2012 1:17 am
Contact:

Re: merge two arrays

Post by Newbie4 » Fri Jul 05, 2013 8:00 am

In your example, this will concatenate the contents of each element of tab2 to each element of tab1, then replace it in tab2:

Code: Select all

     repeat with i = 1 to the number of elements of tab1
           put (tab1[i] & tab2[i]) into tab2[i]
     end repeat
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

kimberlyn
Posts: 18
Joined: Mon Jul 01, 2013 1:40 pm

Re: merge two arrays

Post by kimberlyn » Fri Jul 05, 2013 9:27 am

Thanks !

Another question, how to I merge to strings ?
I have a table of strings. Each string is between 1 and 4 caracters, and I want them all to be 6 caracters long.

I've done that but it doesn't work :

Code: Select all

put "agf" & cr after tab1
put "b" & cr after tab1   
put "cggg" & cr after tab1

split tab1 with cr

   repeat with i = 1 to the number of lines in the keys of tab1
      repeat while the number of char of tab[i] is not 6
         put "-" after tab1[i]
      end repeat
   end repeat   
I want

Code: Select all

agf---
b-----
cggg--
it gives me

Code: Select all

agf
b
cggg
Thanks !

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4163
Joined: Sun Jan 07, 2007 9:12 pm

Re: merge two arrays

Post by bn » Fri Jul 05, 2013 9:53 am

it works if you name your array in the second repeat loop correctly (tab1 instead of tab and if you say "the number of chars" instead of "the number of char"

Code: Select all

put "agf" & cr after tab1
   put "b" & cr after tab1   
   put "cggg" & cr after tab1
   
   split tab1 with cr
   
      repeat with i = 1 to the number of lines in the keys of tab1
            repeat while the number of chars of tab1[i] is not 6
                  put "-" after tab1[i]
            end repeat
      end repeat    
Kind regards
Bernd

shaosean
Posts: 906
Joined: Thu Nov 04, 2010 7:53 am

Re: merge two arrays

Post by shaosean » Fri Jul 05, 2013 9:53 am

Code: Select all

constant kDashes = "------"

put "agf" & cr after tab1
put "b" & cr after tab1   
put "cggg" & cr after tab1

split tab1 with cr

repeat for each key tKey in tab1
  put char 1 to 6 of (tab1[tKey] & kDashes) into tab1[tKey]
end repeat
ps.. you have a typo in yours which is way it probably didn't work, the repeat line you refer to tab instead of tab1

Post Reply