Author Topic: Functions and arrays  (Read 880 times)

Offline Galactygon

  • Modding since 2002
  • Planewalker
  • *****
  • Posts: 378
  • Gender: Male
  • Creator of spells
Functions and arrays
« on: April 11, 2017, 03:20:09 AM »
How exactly are arrays remembered when generated within a function?

I have an example of:
Code: [Select]
DEFINE_ACTION_FUNCTION "PRINT_TEST"
STR_VAR print_list = ""
done_deal = "DEFAULT"
RET done_deal
BEGIN
PRINT "%print_list%"
ACTION_PHP_EACH "%print_list%" AS one => two BEGIN
PRINT "%one% => %two%"
END

ACTION_DEFINE_ASSOCIATIVE_ARRAY "%done_deal%" BEGIN
U => V
W => X
Y => Z
END
END

ACTION_DEFINE_ASSOCIATIVE_ARRAY "TEST" BEGIN
A => B
C => D
E => F
END

LAF "PRINT_TEST" STR_VAR print_list = "TEST" RET done_deal END

PRINT "%done_deal%"
ACTION_PHP_EACH "%done_deal%" AS three => four BEGIN
PRINT "%three% => %four%"
END

This will print the contents of array "TEST" (i.e. "TEST" %WNL% A => B %WNL% C => D %WNL% E => F) but the array "DEFAULT" (U => V, W => X, Y => Z) will not be printed, only the letters "DEFAULT".

Is there a way to return arrays generated within a function like any other variable?

Offline Wisp

  • Moderator
  • Planewalker
  • *****
  • Posts: 1176
Re: Functions and arrays
« Reply #1 on: April 12, 2017, 01:31:53 PM »
Arrays go out of scope with the function, just like variables (arrays are implemented as variables). You cannot return arrays. You could would not work, regardless, however, as you are defining an array rooted in "DEFAULT", but are returning the value of the variable called "done_deal" (and the value is "DEFAULT" because the function never assigns to "done_deal" so the default value is retained).

Offline Galactygon

  • Modding since 2002
  • Planewalker
  • *****
  • Posts: 378
  • Gender: Male
  • Creator of spells
Re: Functions and arrays
« Reply #2 on: April 12, 2017, 01:50:54 PM »
Thanks for the reply. I guess I could try inlining and reincluding stuff if I get desperate. I have settled with macros for now which work for the stuff I want to do.

 

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)?: