Author Topic: Cannot Set Array with Variable Name  (Read 1928 times)

Abel (unapproved)

  • Guest
Cannot Set Array with Variable Name
« on: July 26, 2016, 08:36:43 PM »
Apparently, my account is till awaiting for admin approval but this is really bugging me and I can't move forward with my project. I orginally posted the problem there https://forums.beamdog.com/discussion/comment/798573/#Comment_798573 but I think I'll have more chance catching Wisp here.

Here's a bit of additional code for testing purpose:
Code: [Select]
SPRINT new_array ~src_cre_hea~

SET $src_cre_hea(1)
SET $src_cre_hea(1 2) = 1
SET $src_cre_hea(2) = 1
SET EVAL $~%new_array%~(~%src_cre_hea_2%~) = 2
SET EVAL ~%new_array%_2~ = 2
SET EVAL $~%new_array%~(1 2) = 2

PHP_EACH ~%new_array%~ AS key => val BEGIN
PATCH_PRINT ~%key_0% %key_1% => %val%~
END

What's the problem? The SET EVAL... won't work unless the SET $... with corresponding keys have been made before.
So, for example SET EVAL $~%new_array%~(1 2) = 2 won't be added to the src_cre_hea array and won't be printed, unless SET $src_cre_hea(1 2) = 1 appears before. If so, the new value of 2 will be set.
Trouble is, I'm using all this for functions/macros and not being able to use variable names to set new entries defeats the purpose. I'm sure there are workarounds but undoubtledly messy ones.

And what's with all these verifications? The damn letters are unreadable!

Offline Wisp

  • Moderator
  • Planewalker
  • *****
  • Posts: 1176
Re: Cannot Set Array with Variable Name
« Reply #1 on: July 27, 2016, 01:24:01 PM »
Because reasons, EVAL and the array construct interact in such a way that while the correctly evaluated variable is set to the correct value (i.e., you can do PRINT "%src_cre_hea_1_2%" and have it print 2), the variable is not registered as being part of an array, so PHP_EACH cannot find it.

Offline Abel

  • Planewalker
  • *****
  • Posts: 6
Re: Cannot Set Array with Variable Name
« Reply #2 on: July 27, 2016, 03:12:28 PM »
Thanks for your answer, Wisp. I found workarounds but the resulting code is quite ugly.
I still don't understand why EVAL would be needed in the first place (it's not needed for what's between parentheses). Truth be told I never know how to use it and just throw it at random when something doesn't work.
But back on the matter is there something you can do so that it works the way I described?


Offline Wisp

  • Moderator
  • Planewalker
  • *****
  • Posts: 1176
Re: Cannot Set Array with Variable Name
« Reply #3 on: July 27, 2016, 03:31:26 PM »
Nothing simple, no. Addressing this bug comes with an estimated 110 % chance of causing unintended consequences of the obscure and/or non-obvious kind.

Offline Abel

  • Planewalker
  • *****
  • Posts: 6
Re: Cannot Set Array with Variable Name
« Reply #4 on: July 27, 2016, 03:47:45 PM »
I see. Well, we wouldn't want that now, would we?
:'(

Offline Wisp

  • Moderator
  • Planewalker
  • *****
  • Posts: 1176
Re: Cannot Set Array with Variable Name
« Reply #5 on: July 29, 2016, 01:43:14 PM »
Thinking a bit more about this, the only way I see of fixing it is by having the array construct automatically evaluate variables in the array name (because EVAL gets the result of the array construct, not the other way around, so the array construct could never work with variables without this change). This is a less invasive fix and should hopefully not cause problems.

Offline Abel

  • Planewalker
  • *****
  • Posts: 6
Re: Cannot Set Array with Variable Name
« Reply #6 on: July 30, 2016, 08:40:25 AM »
Well, if that's possible, that would open a lot of options for arrays in macros!

Do we actually need EVAL at all? If I get it right, it simply tells WeiDU to evaluate strings inside %% as variables. So, always using it would only be problematic with strings that don't contain variables but have double %. Doesn't this happen less often? In that case, a NOT_EVAL could be used.

On another note, the array construct tutorial says:
Code: [Select]
OUTER_SET $s(x) = 4
  // equivalent to OUTER_SET ~s_x~ = 4
But it's not really equivalent, since you can't PHP_EACH the ~s_x~ option.
However, if you do this:
Code: [Select]
OUTER_SET ~s_x~ = 4
OUTER_SET $s(x) = 5
The value will be 5. And it will be, of course read by PHP_EACH since you set it with $....
Conclusion: be careful when using "_" to set variable names.

Offline subtledoctor

  • Planewalker
  • *****
  • Posts: 131
Re: Cannot Set Array with Variable Name
« Reply #7 on: June 24, 2017, 11:18:53 PM »
Thread-O Necro!  (I'm sorry for necroing, but it seems the easiest way to ask this question.)  I'm writing some stuff now that uses variable in array names, and I decided to quickly google whether that will actually work, and found this thread (posted circa Weidu v237), suggesting that it didn't work in mid-2016, but making it work was under consideration.  So:

Thinking a bit more about this, the only way I see of fixing it is by having the array construct automatically evaluate variables in the array name (because EVAL gets the result of the array construct, not the other way around, so the array construct could never work with variables without this change). This is a less invasive fix and should hopefully not cause problems.

Was this ever implemented? 

Offline Wisp

  • Moderator
  • Planewalker
  • *****
  • Posts: 1176
Re: Cannot Set Array with Variable Name
« Reply #8 on: June 26, 2017, 05:22:14 AM »
Yes.

Offline GeN1e

  • Planewalker
  • *****
  • Posts: 267
  • Gender: Male
Re: Cannot Set Array with Variable Name
« Reply #9 on: July 07, 2017, 04:29:02 PM »
Just for the record, this has been working fine for years

SET $ EVAL ~%array%~("%key%") = 1

 

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