Arrays: new and old keys, i

David Bovill david at vaudevillecourt.tv
Sat Sep 13 06:41:51 EDT 2008


2008/9/13 Mark Wieder <mwieder at ahsoftware.net>

>
> Still not getting it here. I often do that with arrays, but I just use
> a multipart key (and now I can use multidimensional array keys). I've
> never needed to retrieve elements in the order added and still can't
> see the utility of it. The following not only preserves title order,
> but frees you from having to add them in order...
>
> put firstTitle into tArray[1,"title"]
> put secondTitle into tArray[2,"title"]
> put fourthTitle into tArray[4,"title"]
> put thirdTitle into tArray[3,"title"]
>
> put the keys of tArray into tKeys
> filter tKeys with "*,title"
> sort tKeys


Yes. You can do this. Using multipart arrays is probably better than my
usual technique of simply building an ordered index separately, and possibly
adding it to the array (if I need to pass the data around a lot) - under
some safe named key like "_SortedIndex". I guess you have the same problem
when you want to generate indexes from the keys - you'd have to extract the
second column from the keys - I have to delete the line "_SortedIndex"... so
yes it is doable and not really "hard". So I think we are on the same wave
length. We differ in that I'm complaining about it :)

For beginners - it is counter intuitive (I remember having to ask on list
about it when learning - and takes up space explaining in the
documentation).  It takes a lot of lines of code, and messes up otherwise
nice clean data structures with hacks just to get around the fact that you
don't key your keys back in the order you put it in, and it is a very common
task. I want my arrays to look like;

put firstTitle into tArray["title"]
put secondTitle into tArray["title"]
put fourthTitle into tArray["title"]
put thirdTitle into tArray["title"]

and not

put firstTitle into tArray[1,"title"]
put secondTitle into tArray[2,"title"]
put fourthTitle into tArray[4,"title"]
put thirdTitle into tArray[3,"title"]

whereever possible :) Multidimesnsional arrays do my head in. Why would
anyone want the reverse?

Just to change the subject - how about making it easier to create text
string containing quotes - that's my number 1 grumble regarding elegant,
easy to write code. Having to hack arrays to get indexes back in the order i
put them in in only number 2.



More information about the use-livecode mailing list