keeping keys of an array in the order they came in?

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

keeping keys of an array in the order they came in?

Ben Rubinstein via use-livecode
Hi dfriends,

need a little help, I am filling an array (to eliminate doublettes)
and really need to keep the order of the keys as they came in.

Example:
...
put "60,30,40" into tK
repeat for each item tItem in tK
put "sdsdsdsddsd" into tArray[tItem]
end repeat
put the keys of tArray
...
Now I would like to get:
60
30
40

This is just an example, I cannot control the content of tK.
Know what I mean?

Any hints very welcome, thank you!


Best

Klaus

--
Klaus Major
http://www.major-k.de
[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: keeping keys of an array in the order they came in?

Ben Rubinstein via use-livecode
Maybe a two-part array?

Myarray[1,tk]

You can control the first part of the array, keeping it in order, and thus not depend on how LC orders the tK part of the array.

Sent from my iPhone

> On Aug 19, 2017, at 7:33 AM, Klaus major-k via use-livecode <[hidden email]> wrote:
>
> Hi dfriends,
>
> need a little help, I am filling an array (to eliminate doublettes)
> and really need to keep the order of the keys as they came in.
>
> Example:
> ...
> put "60,30,40" into tK
> repeat for each item tItem in tK
> put "sdsdsdsddsd" into tArray[tItem]
> end repeat
> put the keys of tArray
> ...
> Now I would like to get:
> 60
> 30
> 40
>
> This is just an example, I cannot control the content of tK.
> Know what I mean?
>
> Any hints very welcome, thank you!
>
>
> Best
>
> Klaus
>
> --
> Klaus Major
> http://www.major-k.de
> [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

_______________________________________________
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: keeping keys of an array in the order they came in?

Ben Rubinstein via use-livecode
In reply to this post by Ben Rubinstein via use-livecode
Hi Klaus
I've been there and the only solution I found was to maintain
a second array :

  put 0 into x
  put "abc" into array1[60]
  add 1 to x
  put 60 into array2[x]
  put "xyz" into array1[30]
  add 1 to x
  put 30 into array2[x]

  put the keys of array2 into tkeys
  sort lines of tkeys ascending numeric
  repeat for each line j in tkeys
    put array1[array2[j]] & return after myvar
  end repeat

You can also use a two-dimentional array instead
of 2 separate arrays...

jbv


On Sat, August 19, 2017 1:33 pm, Klaus major-k via use-livecode wrote:

> Hi dfriends,
>
>
> need a little help, I am filling an array (to eliminate doublettes) and
> really need to keep the order of the keys as they came in.
>
> Example:
> ...
> put "60,30,40" into tK repeat for each item tItem in tK put "sdsdsdsddsd"
> into tArray[tItem] end repeat put the keys of tArray ...
> Now I would like to get:
> 60
> 30
> 40
>
>
> This is just an example, I cannot control the content of tK.
> Know what I mean?
>
>
> Any hints very welcome, thank you!
>
>
>
> Best
>
>
> Klaus
>
>
> --
> Klaus Major
> http://www.major-k.de
> [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
>
>



_______________________________________________
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: keeping keys of an array in the order they came in?

Ben Rubinstein via use-livecode
You could alter the name of the elements...

Put "X_" & tK into tArrayElement

Put tData into myArray[tArrayElement]

X would be created in a counting loop.

That way, when you get the keys of the array, you can sort numeric by item 1 of each, using "_" as the itemdel. This will give you the original order with just 1 array dimension, and you can easily retrieve the value of tK.

Sent from my iPhone

> On Aug 19, 2017, at 8:12 AM, jbv via use-livecode <[hidden email]> wrote:
>
> Hi Klaus
> I've been there and the only solution I found was to maintain
> a second array :
>
>  put 0 into x
>  put "abc" into array1[60]
>  add 1 to x
>  put 60 into array2[x]
>  put "xyz" into array1[30]
>  add 1 to x
>  put 30 into array2[x]
>
>  put the keys of array2 into tkeys
>  sort lines of tkeys ascending numeric
>  repeat for each line j in tkeys
>    put array1[array2[j]] & return after myvar
>  end repeat
>
> You can also use a two-dimentional array instead
> of 2 separate arrays...
>
> jbv
>
>
>> On Sat, August 19, 2017 1:33 pm, Klaus major-k via use-livecode wrote:
>> Hi dfriends,
>>
>>
>> need a little help, I am filling an array (to eliminate doublettes) and
>> really need to keep the order of the keys as they came in.
>>
>> Example:
>> ...
>> put "60,30,40" into tK repeat for each item tItem in tK put "sdsdsdsddsd"
>> into tArray[tItem] end repeat put the keys of tArray ...
>> Now I would like to get:
>> 60
>> 30
>> 40
>>
>>
>> This is just an example, I cannot control the content of tK.
>> Know what I mean?
>>
>>
>> Any hints very welcome, thank you!
>>
>>
>>
>> Best
>>
>>
>> Klaus
>>
>>
>> --
>> Klaus Major
>> http://www.major-k.de
>> [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
>>
>>
>
>
>
> _______________________________________________
> 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: keeping keys of an array in the order they came in?

Ben Rubinstein via use-livecode
Hi Jonathan,

> Am 19.08.2017 um 14:22 schrieb Jonathan Lynch via use-livecode <[hidden email]>:
>
> You could alter the name of the elements...
>
> Put "X_" & tK into tArrayElement
> Put tData into myArray[tArrayElement]
> X would be created in a counting loop.
>
> That way, when you get the keys of the array, you can sort numeric by item 1 of each, using "_" as the itemdel. This will give you the original order with just 1 array dimension, and you can easily retrieve the value of tK.

not possible, I am doing something like this:
...
put empty into tArray
repeat for each line tLine in tData
put item 2 of tLine into tKey
if tKey = EMPTY then next repeat
put tLine & CR after tArray[tKey]
end repeat
...
And item 2 are these said number, so I cannot control/name the key this way.

> Sent from my iPhone
>
>> On Aug 19, 2017, at 8:12 AM, jbv via use-livecode <[hidden email]> wrote:
>>
>> Hi Klaus
>> I've been there and the only solution I found was to maintain
>> a second array :
>>
>> put 0 into x
>> put "abc" into array1[60]
>> add 1 to x
>> put 60 into array2[x]
>> put "xyz" into array1[30]
>> add 1 to x
>> put 30 into array2[x]
>>
>> put the keys of array2 into tkeys
>> sort lines of tkeys ascending numeric
>> repeat for each line j in tkeys
>>   put array1[array2[j]] & return after myvar
>> end repeat

Best

Klaus

--
Klaus Major
http://www.major-k.de
[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: keeping keys of an array in the order they came in?

Ben Rubinstein via use-livecode
Have a separate list, or a special key that holds your list of keys.

put empty into tArray
repeat for each line tLine in tData
put item 2 of tLine into tKey
if tKey = EMPTY then next repeat
put tLine & CR after tArray[tKey]
put tKey & cr after tarray['myKeyList']
end repeat

Then when you need to pull things back out, grab myKeyList and iterated
through it.  (delete the trailing cr after the repeat ends of course)

On Sat, Aug 19, 2017 at 6:28 AM, Klaus major-k via use-livecode <
[hidden email]> wrote:

> Hi Jonathan,
>
> > Am 19.08.2017 um 14:22 schrieb Jonathan Lynch via use-livecode <
> [hidden email]>:
> >
> > You could alter the name of the elements...
> >
> > Put "X_" & tK into tArrayElement
> > Put tData into myArray[tArrayElement]
> > X would be created in a counting loop.
> >
> > That way, when you get the keys of the array, you can sort numeric by
> item 1 of each, using "_" as the itemdel. This will give you the original
> order with just 1 array dimension, and you can easily retrieve the value of
> tK.
>
> not possible, I am doing something like this:
> ...
> put empty into tArray
> repeat for each line tLine in tData
> put item 2 of tLine into tKey
> if tKey = EMPTY then next repeat
> put tLine & CR after tArray[tKey]
> end repeat
> ...
> And item 2 are these said number, so I cannot control/name the key this
> way.
>
> > Sent from my iPhone
> >
> >> On Aug 19, 2017, at 8:12 AM, jbv via use-livecode <
> [hidden email]> wrote:
> >>
> >> Hi Klaus
> >> I've been there and the only solution I found was to maintain
> >> a second array :
> >>
> >> put 0 into x
> >> put "abc" into array1[60]
> >> add 1 to x
> >> put 60 into array2[x]
> >> put "xyz" into array1[30]
> >> add 1 to x
> >> put 30 into array2[x]
> >>
> >> put the keys of array2 into tkeys
> >> sort lines of tkeys ascending numeric
> >> repeat for each line j in tkeys
> >>   put array1[array2[j]] & return after myvar
> >> end repeat
>
> Best
>
> Klaus
>
> --
> Klaus Major
> http://www.major-k.de
> [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
>
_______________________________________________
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: keeping keys of an array in the order they came in?

Ben Rubinstein via use-livecode
Hi Mike,

> Am 19.08.2017 um 14:36 schrieb Mike Bonner via use-livecode <[hidden email]>:
>
> Have a separate list, or a special key that holds your list of keys.
>
> put empty into tArray
> repeat for each line tLine in tData
> put item 2 of tLine into tKey
> if tKey = EMPTY then next repeat
> put tLine & CR after tArray[tKey]
> put tKey & cr after tarray['myKeyList']
> end repeat
>
> Then when you need to pull things back out, grab myKeyList and iterated
> through it.  (delete the trailing cr after the repeat ends of course)

thank you, will try this!

> On Sat, Aug 19, 2017 at 6:28 AM, Klaus major-k via use-livecode <
> [hidden email]> wrote:
>
>> Hi Jonathan,
>>
>>> Am 19.08.2017 um 14:22 schrieb Jonathan Lynch via use-livecode <
>> [hidden email]>:
>>>
>>> You could alter the name of the elements...
>>>
>>> Put "X_" & tK into tArrayElement
>>> Put tData into myArray[tArrayElement]
>>> X would be created in a counting loop.
>>>
>>> That way, when you get the keys of the array, you can sort numeric by
>> item 1 of each, using "_" as the itemdel. This will give you the original
>> order with just 1 array dimension, and you can easily retrieve the value of
>> tK.
>>
>> not possible, I am doing something like this:
>> ...
>> put empty into tArray
>> repeat for each line tLine in tData
>> put item 2 of tLine into tKey
>> if tKey = EMPTY then next repeat
>> put tLine & CR after tArray[tKey]
>> end repeat
>> ...
>> And item 2 are these said number, so I cannot control/name the key this
>> way.

Best

Klaus
--
Klaus Major
http://www.major-k.de
[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: keeping keys of an array in the order they came in?

Ben Rubinstein via use-livecode
Hi Mike,

I added a little IF THEN clause to avoid doublettes in the ney key, too,
and now it does what I need, thanks again!

> Am 19.08.2017 um 15:12 schrieb Klaus major-k via use-livecode <[hidden email]>:
>>
>> put empty into tArray
>> repeat for each line tLine in tData
>> put item 2 of tLine into tKey
>> if tKey = EMPTY then next repeat
>> put tLine & CR after tArray[tKey]

if lineoffset(tKey,tArray["keylist"]) = 0 then
 put tKey & CR after tArray["keylist"]
end if

>> ## put tKey & cr after tarray['myKeyList']
>> end repeat

Best

Klaus

--
Klaus Major
http://www.major-k.de
[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: keeping keys of an array in the order they came in?

Ben Rubinstein via use-livecode
Little addition, thanks hermann:

> Am 19.08.2017 um 15:44 schrieb Klaus major-k via use-livecode <[hidden email]>:
>
> Hi Mike,
>
> I added a little IF THEN clause to avoid doublettes in the ney key, too,
> and now it does what I need, thanks again!
>
>> Am 19.08.2017 um 15:12 schrieb Klaus major-k via use-livecode <[hidden email]>:
>>>
>>> put empty into tArray

## To avoid surpises with LINEOFFSET:
set the wholematches to TRUE

>>> repeat for each line tLine in tData
>>> put item 2 of tLine into tKey
>>> if tKey = EMPTY then next repeat
>>> put tLine & CR after tArray[tKey]

## So this will work as exspected:
>
> if lineoffset(tKey,tArray["keylist"]) = 0 then
> put tKey & CR after tArray["keylist"]
> end if
>
>>> ## put tKey & cr after tarray['myKeyList']
>>> end repeat

Best

Klaus

--
Klaus Major
http://www.major-k.de
[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: keeping keys of an array in the order they came in?

Ben Rubinstein via use-livecode

Hmmm - you haven't mentioned how much data you're working with.
Using lineoffset() to check for duplicated keys will make you
(relatively) slow on large data sets, since it is O(n**2).

Since you know that item 2 is not empty, you must also know that the
whole line is not empty :-),
so you can simply check for that.

put empty into Array
repeat for each line tLine in tData
   put item 2 of tLine into tKey
   if tKey = EMPTY then next repeat
   if tArray[tKey] is EMPTY then
      put tLine & CR after tArray[tKey]   -- or would 'into' be even
faster than 'after' ??
   end if
end repeat

Alex.

On 19/08/2017 15:23, Klaus major-k via use-livecode wrote:

> Little addition, thanks hermann:
>
>> Am 19.08.2017 um 15:44 schrieb Klaus major-k via use-livecode <[hidden email]>:
>>
>> Hi Mike,
>>
>> I added a little IF THEN clause to avoid doublettes in the ney key, too,
>> and now it does what I need, thanks again!
>>
>>> Am 19.08.2017 um 15:12 schrieb Klaus major-k via use-livecode <[hidden email]>:
>>>> put empty into tArray
> ## To avoid surpises with LINEOFFSET:
> set the wholematches to TRUE
>
>>>> repeat for each line tLine in tData
>>>> put item 2 of tLine into tKey
>>>> if tKey = EMPTY then next repeat
>>>> put tLine & CR after tArray[tKey]
> ## So this will work as exspected:
>> if lineoffset(tKey,tArray["keylist"]) = 0 then
>> put tKey & CR after tArray["keylist"]
>> end if
>>
>>>> ## put tKey & cr after tarray['myKeyList']
>>>> end repeat
> Best
>
> Klaus
>
> --
> Klaus Major
> http://www.major-k.de
> [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



_______________________________________________
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: keeping keys of an array in the order they came in?

Ben Rubinstein via use-livecode
Hi Alex,

> Am 19.08.2017 um 16:57 schrieb Alex Tweedly via use-livecode <[hidden email]>:
>
> Hmmm - you haven't mentioned how much data you're working with.

not sure, but definitively less than 5000.

> Using lineoffset() to check for duplicated keys will make you (relatively) slow on large data sets, since it is O(n**2).

I made little test and it is fast enough for my purpose. :-)

> Since you know that item 2 is not empty, you must also know that the whole line is not empty :-),
> so you can simply check for that.
>
> put empty into Array
> repeat for each line tLine in tData
>  put item 2 of tLine into tKey
>  if tKey = EMPTY then next repeat
> # if tArray[tKey] is EMPTY then
>     put tLine & CR after tArray[tKey]   -- or would 'into' be even faster than 'after' ??

## I need AFTER, since I want to collect all lines with the same item 2 in that key.
## So no need for this IF clause in my case.

> # end if
> end repeat
>
> Alex.

Best

Klaus

--
Klaus Major
http://www.major-k.de
[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