put one array after another

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

put one array after another

Matt Maier
Is there a command to merge two array variables into one?

Example:

tFirstArray[tom] = mot
tFirstArray[jane] = enaj

tSecondArray[bill] = llib
tSecondArray[name] = eman

put tSecondArray after tFirstArray

tFirstArray[tom] = mot
tFirstArray[jane] = enaj
tFirstArray[bill] = llib
tFirstArray[name] = eman
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: put one array after another

Tore Nilsen
You could try this, it works if the array is declared global or local, and should work if  the arrays are script local if both arrays are constructed within the same handler

repeat for each key tKey in tSecondArray

put tSecondArray[tKey] into tFirstArray[tKey]

end repeat


Regards
Tore


> 10. aug. 2016 kl. 18.41 skrev Matt Maier <[hidden email]>:
>
> Is there a command to merge two array variables into one?
>
> Example:
>
> tFirstArray[tom] = mot
> tFirstArray[jane] = enaj
>
> tSecondArray[bill] = llib
> tSecondArray[name] = eman
>
> put tSecondArray after tFirstArray
>
> tFirstArray[tom] = mot
> tFirstArray[jane] = enaj
> tFirstArray[bill] = llib
> tFirstArray[name] = eman
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: put one array after another

Matt Maier
Thanks Tore, yeah that works. I was just curious if there was a way to do
it directly. Anytime the syntax is simpler there are fewer chances to make
a mistake.

I found "append" in the dictionary but it's not really documented and I
couldn't get a script to compile with it.

On Wed, Aug 10, 2016 at 7:51 PM, Tore Nilsen <[hidden email]> wrote:

> You could try this, it works if the array is declared global or local, and
> should work if  the arrays are script local if both arrays are constructed
> within the same handler
>
> repeat for each key tKey in tSecondArray
>
> put tSecondArray[tKey] into tFirstArray[tKey]
>
> end repeat
>
>
> Regards
> Tore
>
>
> > 10. aug. 2016 kl. 18.41 skrev Matt Maier <[hidden email]>:
> >
> > Is there a command to merge two array variables into one?
> >
> > Example:
> >
> > tFirstArray[tom] = mot
> > tFirstArray[jane] = enaj
> >
> > tSecondArray[bill] = llib
> > tSecondArray[name] = eman
> >
> > put tSecondArray after tFirstArray
> >
> > tFirstArray[tom] = mot
> > tFirstArray[jane] = enaj
> > tFirstArray[bill] = llib
> > tFirstArray[name] = eman
> > _______________________________________________
> > use-livecode mailing list
> > [hidden email]
> > Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> > http://lists.runrev.com/mailman/listinfo/use-livecode
>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: put one array after another

Tore Nilsen
I don’t think append is a command, it is used when you open files to add new data at the end of the file.

 — open file myFilePath for append

Tore

> 10. aug. 2016 kl. 19.05 skrev Matt Maier <[hidden email]>:
>
> Thanks Tore, yeah that works. I was just curious if there was a way to do
> it directly. Anytime the syntax is simpler there are fewer chances to make
> a mistake.
>
> I found "append" in the dictionary but it's not really documented and I
> couldn't get a script to compile with it.
>
> On Wed, Aug 10, 2016 at 7:51 PM, Tore Nilsen <[hidden email]> wrote:
>
>> You could try this, it works if the array is declared global or local, and
>> should work if  the arrays are script local if both arrays are constructed
>> within the same handler
>>
>> repeat for each key tKey in tSecondArray
>>
>> put tSecondArray[tKey] into tFirstArray[tKey]
>>
>> end repeat
>>
>>
>> Regards
>> Tore
>>
>>
>>> 10. aug. 2016 kl. 18.41 skrev Matt Maier <[hidden email]>:
>>>
>>> Is there a command to merge two array variables into one?
>>>
>>> Example:
>>>
>>> tFirstArray[tom] = mot
>>> tFirstArray[jane] = enaj
>>>
>>> tSecondArray[bill] = llib
>>> tSecondArray[name] = eman
>>>
>>> put tSecondArray after tFirstArray
>>>
>>> tFirstArray[tom] = mot
>>> tFirstArray[jane] = enaj
>>> tFirstArray[bill] = llib
>>> tFirstArray[name] = eman
>>> _______________________________________________
>>> use-livecode mailing list
>>> [hidden email]
>>> Please visit this url to subscribe, unsubscribe and manage your
>> subscription preferences:
>>> http://lists.runrev.com/mailman/listinfo/use-livecode
>>
>> _______________________________________________
>> use-livecode mailing list
>> [hidden email]
>> Please visit this url to subscribe, unsubscribe and manage your
>> subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
>>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: put one array after another

Colin Holgate-3
In reply to this post by Matt Maier
In other languages there is a concat function to join two arrays. It’s in the glossary, but not there as a function.


> On Aug 10, 2016, at 5:41 PM, Matt Maier <[hidden email]> wrote:
>
> Is there a command to merge two array variables into one?
>
> Example:
>
> tFirstArray[tom] = mot
> tFirstArray[jane] = enaj
>
> tSecondArray[bill] = llib
> tSecondArray[name] = eman
>
> put tSecondArray after tFirstArray
>
> tFirstArray[tom] = mot
> tFirstArray[jane] = enaj
> tFirstArray[bill] = llib
> tFirstArray[name] = eman
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: put one array after another

[-hh]
> Matt Maier wrote:
> Is there a command to merge two array variables into one?

This works for me:
union array1 with array2 [recursively]
Reply | Threaded
Open this post in threaded view
|

Re: put one array after another

Mike Bonner
In reply to this post by Colin Holgate-3
Try union
union firstarray with secondarray
though if you have duplicate keys, the duplicate will not change the
current.  From the dictionary...

local tLeft, tRight put "green" into tLeft["color"] put "left" into tLeft[
"align"] put "blue" into tRight["color"] put "100" into tRight["width"]
union tLeft with tRight # RESULT # tLeft["colour"] = "green" #
tLeft["align"] = "left" # tLeft["width"] = "100" # tRight unchanged

On Wed, Aug 10, 2016 at 11:17 AM, Colin Holgate <[hidden email]>
wrote:

> In other languages there is a concat function to join two arrays. It’s in
> the glossary, but not there as a function.
>
>
> > On Aug 10, 2016, at 5:41 PM, Matt Maier <[hidden email]> wrote:
> >
> > Is there a command to merge two array variables into one?
> >
> > Example:
> >
> > tFirstArray[tom] = mot
> > tFirstArray[jane] = enaj
> >
> > tSecondArray[bill] = llib
> > tSecondArray[name] = eman
> >
> > put tSecondArray after tFirstArray
> >
> > tFirstArray[tom] = mot
> > tFirstArray[jane] = enaj
> > tFirstArray[bill] = llib
> > tFirstArray[name] = eman
> > _______________________________________________
> > use-livecode mailing list
> > [hidden email]
> > Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> > http://lists.runrev.com/mailman/listinfo/use-livecode
>
>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: put one array after another

Bob Sneidar-2
It will if you union both ways.

Bob S


On Aug 10, 2016, at 10:58 , Mike Bonner <[hidden email]<mailto:[hidden email]>> wrote:

Try union
union firstarray with secondarray
though if you have duplicate keys, the duplicate will not change the
current.

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: put one array after another

Monte Goulding-2
In reply to this post by Mike Bonner
I agree that it is union that you want here, however, it is interesting to note that Peter just recently implemented list concatenation using the & operator in LCB. This might have been applicable if you were working with ordered numerically indexed arrays. I’ve been thinking lately about commands for lists in LCS that could work equally well for items, lines and elements of 1…N arrays. Things like:

push <value> onto {front | back} {item | line | element} of <list>
pop from {front | back} {item | line | element} of <list> [into <var>]
append <list> to {items | lines | elements} of <list>

Here <list> is either a string or a 1…N indexed array.

Unfortunately I think we would need an explicit append command rather than using a concatenation operator or put after/before because of the way arrays are silently converted to empty strings.

Cheers

Monte
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: put one array after another

Quentin Long
In reply to this post by Matt Maier

sez Matt Maier <[hidden email]>:

> Is there a command to merge two array variables into one?
>
> Example:
>
> tFirstArray[tom] = mot
> tFirstArray[jane] = enaj
>
> tSecondArray[bill] = llib
> tSecondArray[name] = eman
>
> put tSecondArray after tFirstArray
>
> tFirstArray[tom] = mot
> tFirstArray[jane] = enaj
> tFirstArray[bill] = llib
> tFirstArray[name] = eman

I don't know if there's a command that will do the job. However, there's a construction I use when I merge two list variables into one:

put ItemList2 into item (1 + the number of items in ItemList1) of ItemList1

That construction may seem a little weird, but it does the job. So *if* the same sort of logic applies to arrays, something like this might do the job:

function ConcatArray Array1, Array2
  -- if this was a real function, it would confirm that Array1 and Array2 are both, you know, *arrays*
  put the number of lines in the keys of Array1 into A1
  put the keys of Array2 into key (A1 + 1) of Array1
  return Array1
end ConcatArray

   
"Bewitched" + "Charlie's Angels" - Charlie = "At Arm's Length"
   
Read the webcomic at [ http://www.atarmslength.net ]!
   
If you like "At Arm's Length", support it at [ http://www.patreon.com/DarkwingDude ].

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: put one array after another

Richard Gaskin
Quentin Long wrote:

 > I don't know if there's a command that will do the job. However,
 > there's a construction I use when I merge two list variables into one:
 >
 > put ItemList2 into item (1 + the number of items in ItemList1) of
 > ItemList1
 >
 > That construction may seem a little weird, but it does the job. So
 > *if* the same sort of logic applies to arrays, something like this
 > might do the job:
 >
 > function ConcatArray Array1, Array2
 >   -- if this was a real function, it would confirm that Array1 and
Array2 are both, you know, *arrays*
 >   put the number of lines in the keys of Array1 into A1
 >   put the keys of Array2 into key (A1 + 1) of Array1
 >   return Array1
 > end ConcatArray

The "*if*" there is critical, as strings (LC lists) do not work like arrays.

I'm not sure of the specifics of LC's implementation, but this general
discussion may be useful:
https://en.wikipedia.org/wiki/Associative_array#Implementation

In brief (and woefully oversimplified), we could conceive of an array as
a collection of memory addresses, in which each address is derived from
a hashing function applied to the key.

This is why when we try to display an array in a field it shows empty -
there is no single string for the array data, its contents instead
spread across multiple locations linked together through pointers.
Indeed, given that arrays can be nested, it's non-trivial to come up
with a string representation to meaningfully represent them.*

In contrast to the actual structure of an array, using the keys function
does return a string, a return-delimited list of the key names.  But the
creation of that string is copying the keys from the actual array
structure, and not the structure itself.

So given an array which we could notate as:

   Array2/
     key "a" = value "SomeValue"
     key "b" = value "SomeOtherValue"

...the line above that reads:

   put the keys of Array2 into key (A1 + 1) of Array1

...would first get a string comprised of copies of the key names, like this:

    a
    b

...then add 1 to the number of lines there to get 3, and then use that
as the string list of key names as the value of element Array1[3]

That is, if the syntax "...into key <keyName> of <arrayName>" was
something LC did - using LC we'd need to write that as:

    put the keys of Array2 into Array1[A1+1)

But while that modified line would execute, it still won't do what we
want here.  It applies a return delimited string of key names as a value
to a single element, and what we're looking for is a method of bulk
copying the actual array elements.


This post may seem tediously long and pedantic, but bear with me, as I
think we're discovering an opportunity for an enhanced array tutorial.

The conceptualization of the role of array keys here closely matches one
we saw a couple weeks ago on this list, in which a very experienced
developer was attempting to use the keys of an array as a sort of bulk
copying method for the array elements.

Whether we have a good means of doing that bulk copying already (union
seems useful here) is less interesting to me than the conceptualization
itself.  There may be value exploring ways we might make the
conceptualization of arrays more closely match their actual structure,
hopefully making it easier for us to anticipate how the various syntax
for arrays can and can't be used for a given task.

Many years ago Dar Scott put together a wonderfully animated tutorial on
LiveCode (then "Revolution") Message Mechanics, available here:
http://pages.swcp.com/dsc/revstacks.html

I wonder if we might have a similarly inventive soul among us who may be
able to deliver something as nice for explaining array structure.

As with Dar's stack, this may well be a case where illustrations, esp.
animated ones, might help far more than any explanatory text alone.

Arrays are among the more abstract things in LiveCode, a language
otherwise characterized by an ease of learning afforded through more
concrete structures (objects, chunks - things we can see).  But arrays
are so useful in so many contexts that it seems an excellent tutorial
would be a welcome addition to our community learning resources.


* The challenge of representing associate arrays in a textual form is
infamous; doable, but cumbersome.  JSON is the most popular way to do
this, but being designed specifically for the JavaScript engine it's
notoriously tedious to parse in anything other language.  YAML offers a
much more human-readable/writable alternative, though less commonly used.

Now that JSON is included in LC 8 we do at last have a common means of
translating LC's associative arrays to and from textual form.  But for
the sake of readability, it might be nice if there was a common YAML
library available as well.

--
  Richard Gaskin
  Fourth World Systems
  Software Design and Development for the Desktop, Mobile, and the Web
  ____________________________________________________________________
  [hidden email]                http://www.FourthWorld.com

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: put one array after another

Matt Maier
I second the vote for a YAML library. It makes text and arrays work
together a lot better than JSON.

The way I've been tracking arrays in text for documentation purposes is
basically just a table at heart:

array[first-key][this-key] = whatever
array[first-key][that-key] = foobar
array[2nd-key][some-key] = data
array[2nd-key][another-one] = more data
array[3rd-key][sub-key][new-level] = fake data
array[3rd-key][sub-key][next-level] = probably also fake data
array[3rd-key][sub-key][here-we-go-again] = totally the real data

Usually I'll omit the redundant text, which helps me think of it as a tree

array[first-key][this-key] = whatever
                [that-key] = foobar
array[2nd-key][some-key] = data
              [another-one] = more data
array[3rd-key][sub-key][new-level] = fake data
                       [next-level] = probably also fake data
                       [here-we-go-again] = totally the real data

On Thu, Aug 11, 2016 at 6:14 PM, Richard Gaskin <[hidden email]>
wrote:

> Quentin Long wrote:
>
> > I don't know if there's a command that will do the job. However,
> > there's a construction I use when I merge two list variables into one:
> >
> > put ItemList2 into item (1 + the number of items in ItemList1) of
> > ItemList1
> >
> > That construction may seem a little weird, but it does the job. So
> > *if* the same sort of logic applies to arrays, something like this
> > might do the job:
> >
> > function ConcatArray Array1, Array2
> >   -- if this was a real function, it would confirm that Array1 and
> Array2 are both, you know, *arrays*
> >   put the number of lines in the keys of Array1 into A1
> >   put the keys of Array2 into key (A1 + 1) of Array1
> >   return Array1
> > end ConcatArray
>
> The "*if*" there is critical, as strings (LC lists) do not work like
> arrays.
>
> I'm not sure of the specifics of LC's implementation, but this general
> discussion may be useful:
> https://en.wikipedia.org/wiki/Associative_array#Implementation
>
> In brief (and woefully oversimplified), we could conceive of an array as a
> collection of memory addresses, in which each address is derived from a
> hashing function applied to the key.
>
> This is why when we try to display an array in a field it shows empty -
> there is no single string for the array data, its contents instead spread
> across multiple locations linked together through pointers. Indeed, given
> that arrays can be nested, it's non-trivial to come up with a string
> representation to meaningfully represent them.*
>
> In contrast to the actual structure of an array, using the keys function
> does return a string, a return-delimited list of the key names.  But the
> creation of that string is copying the keys from the actual array
> structure, and not the structure itself.
>
> So given an array which we could notate as:
>
>   Array2/
>     key "a" = value "SomeValue"
>     key "b" = value "SomeOtherValue"
>
> ...the line above that reads:
>
>   put the keys of Array2 into key (A1 + 1) of Array1
>
> ...would first get a string comprised of copies of the key names, like
> this:
>
>    a
>    b
>
> ...then add 1 to the number of lines there to get 3, and then use that as
> the string list of key names as the value of element Array1[3]
>
> That is, if the syntax "...into key <keyName> of <arrayName>" was
> something LC did - using LC we'd need to write that as:
>
>    put the keys of Array2 into Array1[A1+1)
>
> But while that modified line would execute, it still won't do what we want
> here.  It applies a return delimited string of key names as a value to a
> single element, and what we're looking for is a method of bulk copying the
> actual array elements.
>
>
> This post may seem tediously long and pedantic, but bear with me, as I
> think we're discovering an opportunity for an enhanced array tutorial.
>
> The conceptualization of the role of array keys here closely matches one
> we saw a couple weeks ago on this list, in which a very experienced
> developer was attempting to use the keys of an array as a sort of bulk
> copying method for the array elements.
>
> Whether we have a good means of doing that bulk copying already (union
> seems useful here) is less interesting to me than the conceptualization
> itself.  There may be value exploring ways we might make the
> conceptualization of arrays more closely match their actual structure,
> hopefully making it easier for us to anticipate how the various syntax for
> arrays can and can't be used for a given task.
>
> Many years ago Dar Scott put together a wonderfully animated tutorial on
> LiveCode (then "Revolution") Message Mechanics, available here:
> http://pages.swcp.com/dsc/revstacks.html
>
> I wonder if we might have a similarly inventive soul among us who may be
> able to deliver something as nice for explaining array structure.
>
> As with Dar's stack, this may well be a case where illustrations, esp.
> animated ones, might help far more than any explanatory text alone.
>
> Arrays are among the more abstract things in LiveCode, a language
> otherwise characterized by an ease of learning afforded through more
> concrete structures (objects, chunks - things we can see).  But arrays are
> so useful in so many contexts that it seems an excellent tutorial would be
> a welcome addition to our community learning resources.
>
>
> * The challenge of representing associate arrays in a textual form is
> infamous; doable, but cumbersome.  JSON is the most popular way to do this,
> but being designed specifically for the JavaScript engine it's notoriously
> tedious to parse in anything other language.  YAML offers a much more
> human-readable/writable alternative, though less commonly used.
>
> Now that JSON is included in LC 8 we do at last have a common means of
> translating LC's associative arrays to and from textual form.  But for the
> sake of readability, it might be nice if there was a common YAML library
> available as well.
>
> --
>  Richard Gaskin
>  Fourth World Systems
>  Software Design and Development for the Desktop, Mobile, and the Web
>  ____________________________________________________________________
>  [hidden email]                http://www.FourthWorld.com
>
>
> _______________________________________________
> use-livecode mailing list
> [hidden email]
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: put one array after another

pmbrig
On Aug 11, 2016, at 11:37 AM, Matt Maier wrote:

> The way I've been tracking arrays in text for documentation purposes is
> basically just a table at heart:
>
> array[first-key][this-key] = whatever
> array[first-key][that-key] = foobar
> array[2nd-key][some-key] = data
> array[2nd-key][another-one] = more data
> array[3rd-key][sub-key][new-level] = fake data
> array[3rd-key][sub-key][next-level] = probably also fake data
> array[3rd-key][sub-key][here-we-go-again] = totally the real data

This looks like the output of a function I use that was based on something Bob Sneider posted to this list.

function arrayToKeyList @pArray, _theKeyList
   -- from Bob Sneider, use-LC list, originally named altPrintKeys()
   --    adjusted by Peter M. Brigham, [hidden email]
   -- returns a list of all the elements of pArray,
   --    with their keys, in the form of <key sequence> <tab> <element>, eg,
   -- [1] ["runs"] [1] ["style"] ["textColor"] 0,0,0
   -- [1] ["runs"] [1] ["style"] ["textStyle"] bold
   -- [1] ["runs"] [1] ["text"] function
   -- [1] ["runs"] [2] ["style"] ["textColor"] 0,0,0
   -- [1] ["runs"] [2] ["text"] sr pStr
   -- [2] ["runs"] [1] ["style"] ["textColor"] 0,0,0
   -- [2] ["runs"] [1] ["text"]  
   -- [2] ["runs"] [2] ["style"] ["textColor"] 0,127,0  ...
   -- pArray is referenced to avoid duplication of large arrays in memory,
   --    but is not changed by this function
   -- _theKeyList is an internal parameter, used for the recursion
   --    do NOT pass a second parameter when calling this function
   --  keyListToArray() is the inverse function, allowing you to re-create the array
   --    from the key list
   -- requires deleteItem, getDelimiters(), keyListToArray()
   
   put numtochar(11) into vertTab
   put numtochar(30) into altCr
   put the keys of pArray into theKeys
   if line 1 of theKeys is a number then
      sort theKeys numeric
   else
      sort theKeys
   end if
   repeat for each line theKey in theKeys
      if theKey is a number then
         put "[" & theKey & "] " after _theKeyList
      else
         put "[" & quote & theKey & quote & "] " after _theKeyList
      end if
      if pArray[theKey] is an array then
         put pArray[theKey] into theTempArray
         put arrayToKeyList(theTempArray, _theKeyList) after theText
         deleteItem _theKeyList,-1,"] "
         put cr into char -1 of theText
      else
         if theKey is a number then
            put "pArray" && "[" & theKey & "]" into theKeyName
         else
            put "pArray" && "[" & quote & theKey & quote & "]" into theKeyName
         end if
         put value(theKeyName) into theValue
         replace tab with vertTab in theValue
         replace cr with altCr in theValue
         put _theKeyList & tab & theValue & cr after theText
         deleteItem _theKeyList,-1,"] "
      end if
   end repeat
   return theText
end arrayToKeyList

on deleteItem @pList, pItemNbr, pDelim
   -- deletes item pItemNbr of pList, given itemdel = pDelim
   -- based on replaceItem
   -- if pDelim = empty, defaults to tab as the delimiter
   -- pList is referenced, so the original list will be changed
   -- most useful when the itemdel is non-standard,
   --    don't have to set it then reset it in calling script
   -- also, pDelim can be a string of characters, so you could do
   --    pList = "first or ninth or second or third"
   --    deleteItem pList,2," or "
   --    and get "first or second or third"
   -- by Peter M. Brigham, [hidden email] — freeware,
   --    based on a handler on the use-LC list
   -- requires getDelimiters()
   
   if pItemNbr = empty then exit deleteItem
   if pDelim = empty then put tab into pDelim
   if len(pDelim) > 1 then
      put getDelimiters(pList) into tempDel
      if tempDelim begins with "Error" then
         answer "Error in getDelimiters()" & cr & tempDelim
         exit to top
      end if
      replace pDelim with tempDel in pList
   else
      put pDelim into tempDel
   end if
   set the itemdelimiter to tempDel
   delete item pItemNbr of pList
   replace tempDel with pDelim in pList
end deleteItem

function getDelimiters pText, nbrNeeded
   -- returns a cr-delimited list of <nbrNeeded> characters
   --    none of which are found in the variable pText
   -- use for delimiters for, eg, parsing text files, manipulating arrays, etc.
   -- usage: put getDelimiters(pText,2) into tDelims
   --        if tDelims begins with "Error" then exit to top -- or whatever
   --        put line 1 of tDelims into lineDivider
   --        put line 2 of tDelims into itemDivider
   --             etc.
   -- by Peter M. Brigham, [hidden email] — freeware
   
   if pText = empty then return "Error: no text specified."
   if nbrNeeded = empty then put 1 into nbrNeeded -- default 1 delimiter
   put "2,3,4,5,6,7,8,16,17,18,19,20,21,22,23,24,25,26" into baseList
   -- low ASCII values, excluding CR, LF, tab, etc.
   put the number of items of baseList into maxNbr
   if nbrNeeded > maxNbr then return "Error: max" && maxNbr && "delimiters."
   repeat for each item testCharNbr in baseList
      put numtochar(testCharNbr) into testChar
      if testChar is not in pText then
         -- found one, store and get next delim
         put testChar & cr after delimList
         if the number of lines of delimList = nbrNeeded
         then return line 1 to -1 of delimList
         -- done
      end if
   end repeat
   -- if we got this far, there was an error
   put the number of lines of delimList into totalFound
   if totalFound = 0 then
      return "Error: cannot get any delimiters."
   else if totalFound = 1 then
      return "Error: can only get 1 delimiter."
   else
      return "Error: can only get" && totalFound && "delimiters."
   end if
end getDelimiters

function keyListToArray theText
   -- the inverse function for arrayToKeyList()
   -- from Bob Sneider, use-LC list, originally named altKeysToArray()
   --    adjusted by Peter M. Brigham, [hidden email]
   -- takes a list of all the elements of an array,
   --    with their keys, in the form of <key sequence> <tab> <element>,
   --    as created by arrayToKeyList(), eg,
   -- [1] ["runs"] [1] ["style"] ["textColor"] 0,0,0
   -- [1] ["runs"] [1] ["style"] ["textStyle"] bold
   -- [1] ["runs"] [1] ["text"] function
   -- [1] ["runs"] [2] ["style"] ["textColor"] 0,0,0
   -- [1] ["runs"] [2] ["text"] sr pStr
   -- [2] ["runs"] [1] ["style"] ["textColor"] 0,0,0
   -- [2] ["runs"] [1] ["text"]  
   -- [2] ["runs"] [2] ["style"] ["textColor"] 0,127,0
   --    and loads it into an array
   -- requires deleteItem, getDelimiters(), arrayToKeyList()
   --    arrayToKeyList() is the inverse function
   
   put numtochar(11) into vertTab
   put numtochar(30) into altCr
   set the itemdel to tab
   repeat for each line theRecord in theText
      put item 1 of theRecord into theKeyList
      put item 2 of theRecord into theValue
      replace vertTab with tab in theValue
      replace altCr with cr in theValue
      put "put theValue into theArrayA" && theKeyList into theCommand
      do theCommand
   end repeat
   return theArrayA
end keyListToArray

----------

-- Peter

Peter M. Brigham
[hidden email]


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: put one array after another

Bob Sneidar-2
In reply to this post by Richard Gaskin
Not sure if it would help, but I wrote two functions that are able to convert arrays into strings and then back into arrays again. It works with nested arrays too. They could be used to create an array merging function where you could tell a 3rd function how to perform the merge.

Bob S



_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode