renaming 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
shadowslash
Posts: 344
Joined: Tue Feb 24, 2009 6:14 pm
Contact:

renaming arrays

Post by shadowslash » Fri May 21, 2010 8:20 am

hi all, how do i rename:

gTestVariable["OldName"]["MoreSubKey"]["Key"]

to another key on the same variable?

gTestVariable["NewName"]["MoreSubKey"]["Key"]

Thank you for the answers.... Image

P.S.
I have noticed that using keys(arrayVariable) returns the keys in alphabetical order... How do I prevent this from happening? Image
Parañaque, Philippines
Image
Image

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: renaming arrays

Post by Mark » Fri May 21, 2010 11:47 am

shadowslash,

Here's an example that shows how to "rename" a key:

Code: Select all

on mouseUp
     put "x" into gVar["x"]["1"]
     put gVar["x"] into gVar["y"]
     delete variable gVar["x"]
     put the keys of gVar
end mouseUp
Arrays are useful, because they have no particular order at all. Why do you have a problem with the way the engine reports the list of keys?

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

shadowslash
Posts: 344
Joined: Tue Feb 24, 2009 6:14 pm
Contact:

Re: renaming arrays

Post by shadowslash » Fri May 21, 2010 11:57 am

Mark wrote:Arrays are useful, because they have no particular order at all. Why do you have a problem with the way the engine reports the list of keys?
It's because I'm going to use it to store data,

e.g.
Data Blue
Data Green
Data Yellow
Data Red
Data White
Data Black

Let's say those are Chapters in a book. Of course chapters in books shouldn't rely on being alphabetically sorted. Now I'll use keys(data) to retrieve the keys. As like a book, I want it to remain the way I added them to the array so that I can easily reference using the keys() function. If not, an alternative would also be great. Image

EDIT:
Chapters in books might not be the best example because I know chapters can be called like Chapter 1: Data blue, etc. So I'll just re-assure my statement, whatever the example is, I really wish it would remain the same order as how each key was filled in.
Parañaque, Philippines
Image
Image

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: renaming arrays

Post by Mark » Fri May 21, 2010 12:11 pm

shadowslash,

The chapters in a book can be stored in an array, but you need to add additional information. For example, you could add the number of the chapter as an additional key. For example:

Code: Select all

gVarArray // name of var
  [name of chapter 1] // primary key
    // secondary keys:
    [key type] //="chapter"
    [chapter number]
    [chapter data]
    [chapter length]
    [author]
    [number of pages]
    [additional remarks]
  [name of chapter 2] // primary key
    etc.
Or you could use a different approach, which uses the chapter numbers as keys:

Code: Select all

gVarArray // name of var
  [number of chapter] // primary key
    // secondary keys
    [chapter data]
    [chapter length]
    [author]
    [number of pages]
    [additional remarks]
  [number of chapter] // primary key
    etc
Whatever suits you...

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

shadowslash
Posts: 344
Joined: Tue Feb 24, 2009 6:14 pm
Contact:

Re: renaming arrays

Post by shadowslash » Fri May 21, 2010 12:18 pm

Interesting stuff, I managed to do the constant arrangement via:

Code: Select all

gArray
     [Chapters]
          Blue                     <-- THIS IS THE VALUE OF THE [CHAPTERS] KEY
          Yellow                       AS YOU CAN SEE, IT'S A RETURN DELIMITED PLAIN TEXT LIST
          Brown
          Black
          Green
          Red
     [Blue]
          [Summary]
               This is the summary of this chapter.
          [Characters]
               These are the characters that appeared on this chapter.
     [Yellow]
          [Summary]
               This is the summary of this chapter.
          [Characters]
               These are the characters that appeared on this chapter.
     [Brown]
          [Summary]
               This is the summary of this chapter.
          [Characters]
               These are the characters that appeared on this chapter.

etc, and so on...
So I basically keep track of the arrangement of the chapters in just another key called "Chapters".
That way, even if I re-arrange the chapters there, the rev app would still read it from the correct array key. Image
Parañaque, Philippines
Image
Image

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: renaming arrays

Post by Mark » Fri May 21, 2010 12:27 pm

Yes, looks good to me, shadowslash.

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

shadowslash
Posts: 344
Joined: Tue Feb 24, 2009 6:14 pm
Contact:

Re: renaming arrays

Post by shadowslash » Fri May 21, 2010 12:42 pm

If it's okay to tell, I also have a problem DataGrid related which is in this thread:
http://forums.runrev.com/phpBB2/viewtop ... f=8&t=5346

i posted it quite some time ago but haven't had any single reply... any help would be appreciated!
Parañaque, Philippines
Image
Image

Post Reply