Howdy, Stranger!

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

[Harlowe] Removing a value from an array

edited September 2015 in Help! with 2.0
Harlowe's documentation tells us to use (move:) or (subarray:) to remove an element from an array. But both macros work with indexes:
(set: $a to (array: "one", "two"))
(set: $b to (array:))
(move: $a's 1st to $b)

This approach doesn't work for me because I don't know the array position of the value I want to remove. What I want is to remove a value, not a position.

I need a way of removing the value "one" from any possible array containing "one" in any position (for example, both arrays ("one","two") and ("two","one")), and the only input I can provide is the value "one" itself.

Another problem is that (move:) leaves a blank value instead of the removed value. Is there a way to completely remove it, so the array becomes one value shorter?

Current result: ("one","two") -> ("","two")
Desired result: ("one","two") -> ("two")

Is there anything I can do? Thanks!

Comments

  • You can subtract one array from another, which will remove the elements of the second array from the first. To get the result you want
    (set: $x to (a: "one", "two"))
    $x => one,two
    (set: $x to $x - (a: "one"))
    $x => two
    
  • Wow. That's so simple. Now I'm feeling bad for not thinking about it myself. Thanks so much.
  • edited September 2015
    Doesn't this same syntax work on datamaps? I get NaN error. Adding datamaps works as expected.
    (set: $x to (datamap: 1,"one", 2,"two"))
    (set: $x to $x - (datamap: 1,"one"))
    
  • No, it seems that subtraction doesn't work with datamaps. The only way to remove elements from a datamap is to use the (move: into) method.

    BTW In your first example you've got (move: $a's 1st to $b) which will just set the first item of $a to 0. Doing (move: $a's 1st into $b) ,or for a datamap (move: $a's 1 into $b), will remove that item completely.
Sign In or Register to comment.