Creating arrays with multiple datamaps (translation from Harlowe)

I’m running Twine 2.5.1 with Sugarcube 2.36.1

This is part of an ongoing project transfer from Harlowe to sugarcube (to which I am a novice)
I have a number of variable arrays holding datamaps with multiple entries in Harlowe like so:

(set:$drawStatus to 0)
(set:$draw to
(a:
(dm:“name”,“draw 1”,“field1”,2,“field2”,6,“field3”,3,“field4”, 2,“type”,“enc”),
(dm:“name”,“draw 2”,“field1”,2,“field2”,6,“field3”,3,“field4”, 2,“type”,“enc”),
(dm:“name”,“draw 3”,“field1”,2,“field2”,6,“field3”,3,“field4”, 2,“type”,“enc”),
(dm:“name”,“draw 4”,“field1”,2,“field2”,6,“field3”,3,“field4”, 2,“type”,“enc”)
)
)

and it all works fine - but I’ve no clue how to emulate this in Sugarcube which seems to suggest (in the Harlowe to sugarcube manual) something like this:

<<set $array to [ ]>>
<<set $map to new Map([[“name”, “value”]])>>

but this doesn’t work as the [[ ]] instantly change into passage links so I’m lost.
is it something to do with

<<set $dm to {“name” : value, “name” : value }>>

???

First. In the future, please use code blocks when giving code examples.

 
As to your Map initization to link issue:

<<set $map to new Map([["name", "value"]])>>

That doesn’t work as you’d expect because of compiler shenanigans. You simply need to make it no longer look like a link, which you can do by placing whitespace between the square brackets so they aren’t doubled up, thus resembling the basic link markup.

 
For your code example. A 100% literal translation would be something like the following: (notice how no brackets double up causing link issues)

<<set $drawStatus to 0>>
<<set $draw to [
	new Map([
		["name", "draw 1"],
		["field1", 2],
		["field2", 6],
		["field3", 3],
		["field4", 2],
		["type", "enc"]
	]),
	new Map([
		["name", "draw 2"],
		["field1", 2],
		["field2", 6],
		["field3", 3],
		["field4", 2],
		["type", "enc"]
	]),
	new Map([
		["name", "draw 3"],
		["field1", 2],
		["field2", 6],
		["field3", 3],
		["field4", 2],
		["type", "enc"]
	]),
	new Map([
		["name", "draw 4"],
		["field1", 2],
		["field2", 6],
		["field3", 3],
		["field4", 2],
		["type", "enc"]
	])
]>>

Usage: (print the value of field1 of the draw 4 Map)

<<= $draw[3].get("field1")>>
/* JavaScript arrays start at index 0, so index 3 is the fourth member. */

I find that a bit awkward, personally, so I’d recommend just using generic objects instead.

For example, using generic objects might look something like the following:

<<set $draw to {
	draw1 : {
		field1 : 2,
		field2 : 6,
		field3 : 3,
		field4 : 2,
		type   : "enc"
	},
	draw2 : {
		field1 : 2,
		field2 : 6,
		field3 : 3,
		field4 : 2,
		type   : "enc"
	},
	draw3 : {
		field1 : 2,
		field2 : 6,
		field3 : 3,
		field4 : 2,
		type   : "enc"
	},
	draw4 : {
		field1 : 2,
		field2 : 6,
		field3 : 3,
		field4 : 2,
		type   : "enc"
	}
}>>

Usage: (print the value of field1 of the draw4 generic object)

<<= $draw.draw4.field1>>

Thanks @TheMadExile I’ll give that a try :+1: