New Shorthand Syntax for Arrays
June 16, 2025
Onetastic has an update with a Macro language improvement that will make it easier to manage arrays. Previously arrays could be created with Array function or using the bracket operator. Array function didn't allow specifying array keys. It only allowed specifying values. If you wanted to specify keys as well, you could use Array_FromKeysAndValues function, which is difficult to use because keys and values are separated:
$keys = Array("color", "size", "type")
$values = Array("red", "large", "t-shirt")
$product = Array_FromKeysAndValues($keys, $values)
$product["color"] = "red"
$product["size"] = "large"
$product["type"] = "t-shirt"
With the latest update you can either use Array function or the new shorthand bracket ([]) and arrow (=>) operators to specify both keys and values:
$product = Array("color" => "red", "size" => "large", "type" => "t-shirt")
$product = ["color" => "red", "size" => "large", "type" => "t-shirt"]
This makes it much easier to use functions that take arrays with specific keys:
$types["Text Files"] = "*.txt"
$types["Image Files"] = "*.jpg;*.png;*.gif"
$files = FileSystem_ShowOpenFileDialog(false, $types)
$files = FileSystem_ShowOpenFileDialog(false, ["Text Files" => "*.txt", "Image Files" => "*.jpg;*.png;*.gif"])
When creating arrays with standard numeric indices, the keys can be omitted as before.
Looping over arrays with access to keys
Another improvement along with this is the ability to use
foreach to have access to both keys and values of an array. This is also achieved using the arrow operator (=>):
$message = "Product Info:n"
foreach ($key => $value in $product)
$message &= $key & ": " & $value & "n"
ShowMessage($message)
Comments