Posted by: _Luke_
« on: November 08, 2022, 05:08:45 AM »So if I'm understanding this correctly, this proposed ARRAY_IS_SET would function like VARIABLE_IS_SET except it also checks that the variable is a member of the given array?
Basically, yes.
So if I'm understanding this correctly, this proposed ARRAY_IS_SET would function like VARIABLE_IS_SET except it also checks that the variable is a member of the given array?
(Though it's not that the key is '1_2', it's that there are 2 keys, 1 and 2.)Yeah, you are right... I wrote "1_2" because I thought about "%my_array_1_2%" (as an alternative to $"my_array"("1" "2")...)
I think rather than memory clearing, it would be better (if possible) just to have some way to detect if an array element is set. saySounds good.
ARRAY_IS_SET string (string list)
so that ARRAY_IS_SET my_array (1) would return false in Luke's example, but ARRAY_IS_SET my_array (1 2) would return true.
So is there still a feature request?
ACTION_DEFINE_ASSOCIATIVE_ARRAY "my_array" BEGIN
1 , 2 => 3
END
LAF "array_contains"
STR_VAR
"array" = "my_array"
"key" = "1"
RET
"value" // will return 1 (true); should return 0 (false) since the key is "1_2" (not just "1")
END
So is there still a feature request?Probably not, since the following code is now working as intended:
DEFINE_PATCH_FUNCTION "fn" BEGIN
CLEAR_ARRAY "my_array"
SET $"my_array"("Minsc") = 1
SET $"my_array"("Jaheira") = 1
PATCH_FOR_EACH "npc" IN "Minsc" "Jaheira" "Sarevok" BEGIN
LPF "array_contains"
STR_VAR
"array" = "my_array"
"key" = "%npc%"
RET
"value"
END
PATCH_IF "%value%" BEGIN
PATCH_PRINT "%npc% is in my_array"
END
END
END
// Main
OUTER_SET $"my_array"("Sarevok") = 1
OUTER_PATCH "" BEGIN
LPF fn END // Will no longer print ~Sarevok is in my_array~
END
DEFINE_DIMORPHIC_FUNCTION array_contains
STR_VAR array=""//array
key=""
val=""
RET value
BEGIN
OUTER_SET value=0
ACTION_PHP_EACH "%array%" AS k1=>v1 BEGIN
ACTION_IF !("%value%") BEGIN
ACTION_IF ("%key%" STRING_EQUAL "%k1%" || "%key%" STR_EQ "") && ("%val%" STRING_EQUAL "%v1%" || "%val%" STR_EQ "") BEGIN
OUTER_SET value=1
END
END
END
END
DEFINE_DIMORPHIC_FUNCTION array_contains
STR_VAR array=""//array
key=""
val=""
RET value
BEGIN
OUTER_SET value=0
ACTION_PHP_EACH "%array%" AS k1=>v1 BEGIN
ACTION_IF ("%key%" STRING_EQUAL "%k1%" || "%key%" STR_EQ "") && ("%val%" STRING_EQUAL "%v1%" || "%val%" STR_EQ "") BEGIN
OUTER_SET value=1
END
END
END
OK, reflecting further on this I think I retract the request. I can see that it's subtle - and possibly difficult - to implement array clearing given how WEIDU arrays work. And testing a quick array_contains function, I find that WEIDU can check if a string is a key in a 10,000 element array in 0.08 seconds, so the concern about performance is overrated.
DEFINE_DIMORPHIC_FUNCTION "clear_array_entries"
RET_ARRAY "empty_array"
BEGIN
OUTER_SET $"empty_array"("whocares") = 1
END
LAF "clear_array_entries" RET_ARRAY "my_array" = "empty_array" END
should turn an array into an "empty" one (it's not really empty though since it contains a dummy entry to bypass that install-time error).I seem to recall rejecting this request in the past, because I think this is why we have variable scopes and that CLEAR_MEMORY is a relic from the before-time, but presumably you wouldn't be requesting it if functions et al. were sufficient, so what am I missing?
ACTION_CLEAR_ARRAY "patch_data"
ACTION_DEFINE_ASSOCIATIVE_ARRAY "patch_data" BEGIN
// something
END
LAF "make_item" STR_VAR "edits" = "patch_data" END
into thisACTION_CLEAR_ARRAY "patch_data" // Do this just once at the beginning of a component, just in case
WITH_SCOPE BEGIN
ACTION_DEFINE_ASSOCIATIVE_ARRAY "patch_data" BEGIN
// something
END
LAF "make_item" STR_VAR "edits" = "patch_data" END
END
In so doing, you're 100% sure that the array "patch_data" is properly cleared.DEFINE_DIMORPHIC_FUNCTION 2da_inject_array
INT_VAR silent=0//boolean
STR_VAR array=""//array
array_in=""//array
column=""
RET_ARRAY array
BEGIN
OUTER_PATCH "" BEGIN
CLEAR_ARRAY rows
CLEAR_ARRAY columns
TO_UPPER column
LPF 2da_extract_rows_columns STR_VAR array RET_ARRAY rows columns END
LPF array_map STR_VAR array=array_in keymap=toupper RET_ARRAY array_in=array END
PATCH_IF !VARIABLE_IS_SET $columns("%column%") BEGIN
PATCH_IF !silent BEGIN
PATCH_WARN "2da_inject_array: column %column% is absent from 2d array %array%"
END
END ELSE BEGIN
PHP_EACH rows AS row=>discard BEGIN
PHP_EACH columns AS col=>discard BEGIN
PATCH_IF VARIABLE_IS_SET $"%array_in%"("%row%") && "%col%" STR_EQ "%column%" BEGIN
SPRINT $array("%row%" "%col%") $"%array_in%"("%row%")
END ELSE BEGIN
SPRINT $array("%row%" "%col%") $"%array%"("%row%" "%col%")
END
END
END
END
END
END
DEFINE_PATCH_FUNCTION fn BEGIN
CLEAR_ARRAY my_array
$my_array("Minsc")=1
$my_array("Jaheira")=1
PATCH_FOR_EACH npc IN Minsc Jaheira Sarevok BEGIN
PATCH_IF VARIABLE_IS_SET $my_array("%npc%") BEGIN
PATCH_PRINT "%npc% is in my_array"
END
END
END