Page 1 of 1

renaming arrays

Posted: Fri May 21, 2010 8:20 am
by shadowslash
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

Re: renaming arrays

Posted: Fri May 21, 2010 11:47 am
by Mark
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

Re: renaming arrays

Posted: Fri May 21, 2010 11:57 am
by shadowslash
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.

Re: renaming arrays

Posted: Fri May 21, 2010 12:11 pm
by Mark
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

Re: renaming arrays

Posted: Fri May 21, 2010 12:18 pm
by shadowslash
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

Re: renaming arrays

Posted: Fri May 21, 2010 12:27 pm
by Mark
Yes, looks good to me, shadowslash.

Mark

Re: renaming arrays

Posted: Fri May 21, 2010 12:42 pm
by shadowslash
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!