Howdy, Stranger!

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

Remove duplicate values from an array (when you don't know where/how many duplicates may exist)

Hi everyone,

I'm working in Harlowe Twine 2.0.

I am creating an in-game vocabulary assessment that tests players as they progress through the game. I have an array $questCompleted1 which includes a list of datamap variables $vocab1, $vocab2, $vocab3. I am using the array $vocabAssessment to add arrays from completed quests to another array so that I can randomly select a vocab variable to test the players based on completed quests. Now I would like that as players complete additional quests I could add an array from other quests to the array $vocabAssessment. So something like, the following: (set: $vocabAssessment to it + $completedQuest2) where $completedQuest2 is equal to ($vocab3, $vocab4, $vocab5). The issue is that now $vocabAssessment contains $vocab1, $vocab2, $vocab3, $vocab3, $vocab4, $vocab5)... $vocab3 is produced twice. Is there a way to remove duplicates in an array?

Comments

  • Hmm...doing this in Harlowe was tricky. I think it'd be way easier with SugarCube using loops, but I'll show you the solution I have and you can decide from there.

    Here is all the code:
    (set: $vocab1 to (datamap: "Word One", "Definition One", "Word Two", "Definition Two", "Word Three", "Definition Three"))
    (set: $vocab2 to (datamap: "Word Four", "Definition Four", "Word Five", "Definition Five", "Word Six", "Definition Six"))
    (set: $vocab3 to (datamap: "Word Seven", "Definition Seven", "Word Eight", "Definition Eight", "Word Nine", "Definition Nine"))
    (set: $vocab4 to (datamap: "Word Ten", "Definition Ten", "Word Eleven", "Definition Eleven", "Word Twelve", "Definition Twelve"))
    (set: $vocab5 to (datamap: "Word Thirteen", "Definition Thirteen", "Word Fourteen", "Definition Fourteen", "Word Fifteen", "Definition Fifteen"))
    (set: $completedQuest1 to (a: $vocab1, $vocab2, $vocab3))
    (set: $completedQuest2 to (a: $vocab3, $vocab4, $vocab5))
    (set: $vocabAssessment to $completedQuest1)
    (unless: $vocabAssessment contains $completedQuest2's 1st)[Quest 2, vocab 1 added(set: $vocabAssessment to it + (a: $completedQuest2's 1st))]
    (unless: $vocabAssessment contains $completedQuest2's 2nd)[Quest 2, vocab 2 added(set: $vocabAssessment to it + (a: $completedQuest2's 2nd))]
    (unless: $vocabAssessment contains $completedQuest2's 3rd)[Quest 2, vocab 3 added(set: $vocabAssessment to it + (a: $completedQuest2's 3rd))]
    (print: "Done")
    (print: $vocabAssessment's 1st)
    (print: $vocabAssessment's 2nd)
    (print: $vocabAssessment's 3rd)
    (print: $vocabAssessment's 4th)
    (print: $vocabAssessment's 5th)
    (print: $vocabAssessment's 6th)
    

    If you run that you'll see there is no duplication of the contents of $vocab3, and trying to print $vocabAssessment's 6th value throws an error.

    The challenge with this method is its scalability. If you have dozens of quests, and each quest can add up to dozens of $vocab datamaps, you have a lot of writing ahead of you.

    Important note: Make sure you set $vocabAssessment to it + an array, because setting $vocabAssessment to it + a datamap causes errors due to $vocabAssessment being an array and being unable to merge with a datamap. Putting the datamap in a single-element array fixes this by making it so that you're just adding array elements to other array elements.

    If the project is huge, I'd consider SugarCube to make use of loops and whatnot.
  • Awesome! Thanks @Naweth for the help! The project is quite big 18 Quests 500+ vocab words...
Sign In or Register to comment.