0 votes
by (160 points)
retagged by

New Twine 2 user here, still in the first couple days of working with the program. I'm using Harlowe 2.1.0.

I'm trying to create a for loop that runs through each name of a datamap and prints the associated value regardless of the name. Is this possible?

Here's the example of how I feel like the code should work:

(set: $inventory to (dm: "clothes", "rags", "weapon", "dagger"))

(for: each _item, $inventory)
[> (print:_item's ?generic name?)]

Is there a solution to this in the current version of Harlowe, or am I out of luck here?

Thanks in advance for any help!

1 Answer

+1 vote
by (159k points)
selected by
Best answer

You need to pass an Array to the each variation of the (for:) macro, and you can use the (datanames: ) macro to get the Array of keys for your $inventory Datamap.

I believe the following example does what you wanted.

(set: $inventory to (dm: "clothes", "rags", "weapon", "dagger"))

(for: each _key, ...(datanames: $inventory))[
> (print: $inventory's (_key))

warning: take care when adding white-space (space characters, line-breaks) between a macro and it's associated hook (eg. your (for:) macro) as that can result is strange things happening and errors.