Page 1 of 1

merge two arrays

Posted: Thu Jul 04, 2013 11:47 pm
by kimberlyn
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 

Re: merge two arrays

Posted: Fri Jul 05, 2013 12:10 am
by bn
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

Re: merge two arrays

Posted: Fri Jul 05, 2013 1:38 am
by kimberlyn
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

Re: merge two arrays

Posted: Fri Jul 05, 2013 1:42 am
by kimberlyn
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

Re: merge two arrays

Posted: Fri Jul 05, 2013 5:42 am
by snm
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

Re: merge two arrays

Posted: Fri Jul 05, 2013 8:00 am
by Newbie4
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

Re: merge two arrays

Posted: Fri Jul 05, 2013 9:27 am
by kimberlyn
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 !

Re: merge two arrays

Posted: Fri Jul 05, 2013 9:53 am
by bn
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

Re: merge two arrays

Posted: Fri Jul 05, 2013 9:53 am
by shaosean

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