Author Topic: Array of Arrays  (Read 1010 times)

Offline Abel

  • Planewalker
  • *****
  • Posts: 6
Array of Arrays
« on: February 10, 2022, 06:20:55 AM »
I can't seem to be able to reference a whole array, only a given value for a given key. I'd like to give arrays as values to another array keys. Is it possible? Or should I use a 2da instead?

Also, it seems the array construct always needs a key or an index. Isn't possible to make it automatically assign an index when you just specify a list of values? Which would be like DEFINE_ARRAY but dynamic.

Offline Wisp

  • Moderator
  • Planewalker
  • *****
  • Posts: 1176
Re: Array of Arrays
« Reply #1 on: February 13, 2022, 02:36:55 AM »
I can't seem to be able to reference a whole array, only a given value for a given key. I'd like to give arrays as values to another array keys. Is it possible? Or should I use a 2da instead?
You can use a variable for the array name while retrieving array values, provided you use an EVAL:
Code: [Select]
ACTION_DEFINE_ARRAY foo BEGIN zero one two END
ACTION_DEFINE_ARRAY bar BEGIN 123 456 789 END
ACTION_DEFINE_ARRAY baz BEGIN foo bar END

OUTER_SPRINT array $baz(0)
OUTER_SPRINT foo_var $EVAL "%array%"(2) // will be two

OUTER_SPRINT array $baz(1)
OUTER_SET index = 1
OUTER_SPRINT bar_var $EVAL "%array%"("%index%") // will be 456

This can furthermore gracefully be extended to a fairly arbitrary evaluation depth with something like the following:
Code: [Select]
OUTER_SPRINT foobar array // "%array%" still evaluates to bar
OUTER_SPRINT other_bar $EVAL EVAL "%%foobar%%"(2) // will be 789

Quote
Also, it seems the array construct always needs a key or an index. Isn't possible to make it automatically assign an index when you just specify a list of values? Which would be like DEFINE_ARRAY but dynamic.
Can you give an example of what you mean?

Offline subtledoctor

  • Planewalker
  • *****
  • Posts: 131
Re: Array of Arrays
« Reply #2 on: March 01, 2022, 09:44:50 AM »
Also, it seems the array construct always needs a key or an index. Isn't possible to make it automatically assign an index when you just specify a list of values? Which would be like DEFINE_ARRAY but dynamic.
I just do something like
Code: [Select]
OUTER_SET ind = 1
ACTION_FOR_EACH thing IN ~thing_1~ ~thing_2~ ~thing_3~ BEGIN
  OUTER_SPRINT $array(~%ind%~)~%thing%~
  OUTER_SET ind = (ind + 1)
END

Sure you could build in some kind of auto-indexing but how much work would be involved, and how much effort would really be saved?
« Last Edit: March 01, 2022, 09:45:24 AM by subtledoctor »

 

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Name: Email:
Verification:
Type the letters shown in the picture
Listen to the letters / Request another image
Type the letters shown in the picture:
What color is grass?:
What is the seventh word in this sentence?:
What is five minus two (use the full word)?: