Howdy, Stranger!

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

How to add an item to a dataset or array?

Hi folks,

Suppose I have a dataset or array $inventory, with contents "scissors", "pie", and "wallet".

Now, suppose I want to add "string" to $inventory.

What is the correct syntax? So far, the only way I've found to do this is (if $inventory is an array):
(set: $inventory to $inventory + (array:"string"))
But that seems a bit clumsy for everyday use.

Comments

  • Based on the Harlowe documentation I believe that is the way to do it.
  • You can actually shorten it to:

    (set: $inventory to it + (a:"string"))
    Actually, I've since discovered there's a bug where you can just write the following, and Harlowe accepts it:

    (set: $inventory to (a:"A","B","C"))
    (set: $inventory's 4th to "D")
    $inventory (= A,B,C,D)
    This wasn't something I'd desired, but I think I might leave it in because it does have symmetry with the element retrieval and reassignment syntax. For appending to arrays, I guess it necessitates adding a "newlast", property index to accompany "last", so you can write (set: $a's newlast to "D").

    You can also do this with datamaps (but not datasets, for obvious reasons):

    (set: $inventory to (datamap: "Sword","Bronze"))
    (set: $inventory's Shield to "Iron")
    (print:$inventory's Shield) (= Iron)
  • Greyelf and L, thanks.

    Creating a newlast property index would make sense. I'm currently a bit bothered by the lack of a "push" analogue to go with "move from." I assume from the Harlowe documentation that "put into" would overwrite the entire array/dataset.

    An example that looks reasonable, but will probably not do what a new user might expect

    (set: $myFavoriteThings to (dataset:"raindrops","whiskers","kettles"))
    (put: "mittens" into $myFavoriteThings)
    (if: "mittens" is in $myFavoriteThings)[Mittens are among my favorite things!]
    ...Actually, right now, that example for some reason tells me that mittens is not defined when it tries to execute the "Put:" ... I'm not sure I understand how the "put:" macro works after all...
  • Hmm, that seems to be due to a bug that has been fixed in the next release.
Sign In or Register to comment.