Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Empty Array Syntax for Harlowe

How do you create an empty array at the start of a story?

I've been trying:

(set: $bag to (a:[]))

Which doesn't seem to work.

I want the array empty in the beginning so I can have the story print that the bag is empty if the length of the array is zero, but have it print the contents of the array if the array contains strings.  In 1.4.2, I would use:

<<if $bag.length is 0>>The bag is empty.<<else>><<print $bag.join("\n");>><<endif>>

But "Harlowe-izing" it doesn't seem to work.  Any thoughts on what I'm doing wrong?

Comments

  • To create an empty array just use the (array:) or (a:) macro without any parameters.

    (set: $bag to (a:))
    items in bag: (print: $bag.length)
    bag contains: $bag

    (set: $bag's 1st to "banana")(set: $bag's 2st to "apple")
    items in bag: (print: $bag.length)
    bag contains: (print: "\n" + $bag.join("\n"))
  • Going to try this in a moment.  I finally got this to work today:

    (set: $basket to (a:[]) )

    In the next passage:
    (set: $basket to $basket + (a:"pasta"))

    And finally in the last passage:
    (print: "You put on the belt:
    " +
    $basket.join("\n"))

    Clunky but works?
  • (a:[]) does not create an empty array, it creates an array with a single element that contains an empty array, as the following example shows. ($basket will have a length of one, where $bag will have a length of zero)

    (set: $basket to (a:[]))
    (set: $bag to (a:))

    Length of basket: (print: $basket.length)
    Length of bag: (print: $bag.length)
    You can use the \n symbol to add new lines to a string, the following two prints are the same:

    (set: $basket to (a:))
    (set: $basket to $basket + (a:"pasta"))

    (print: "You put on the belt:
    " +
    $basket.join("\n"))

    (print: "You put on the belt:\n" + $basket.join("\n"))
Sign In or Register to comment.