Help With String Abbreviation Method

0 favourites
  • 4 posts
From the Asset Store
Sync animations, animation frames, mirror state, sounds and anything else using these methods.
  • Hello, Construct friends! I need help coming up with a method for abbreviating consecutive tokens in a string with redundant information in a specific way.

    Outlined below are a few examples of the results that I'm trying to achieve.

    Example 1:

    input = "Day 1: Routine A|Day 2: Routine A|Day 3: Routine A|Day 4: Routine A"

    output = "Day 1–4: Routine A"

    Example 2:

    input = "Day 1: Routine A|Day 2: Routine A|Day 3: Routine B|Day 4: Routine B"

    output = "Day 1–2: Routine A|Day 3–4: Routine B"|

    Example 3:

    input = "Day 1: Routine A|Day 2: Routine A|Day 3: Routine B|Day 4: Routine A"

    output = "Day 1–2: Routine A|Day 3: Routine B|Day 4: Routine A"

    Example 4:

    input = "Day 1: Routine A|Day 2: Routine B|Day 3: Routine B|Day 4: Routine A"

    output = "Day 1: Routine A|Day 2–3: Routine B|Day 4: Routine A"

    Any help would be greatly appreciated!

  • My approach would be to push the results into an array, only if that category doesn't already exist in the array (you can check by using array.indexof(category). If it does exist then add one to that index instead.

    Alternatively, you can check if only the last entry was the same, by comparing either the front or back of the array (whichever one you push to), to your current input. If it is the same, increment, and if it is different, push a new cell.

  • I like the approach, but then I run into the problem where the engine compares "Day 2: Routine A" to "Day 1: Routine A", sees that they're technically not the same, and pushes a new cell. How do I get around that?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I figured it out!

    Used a method similar to one which oosyrag suggested, plus a little "coding gymnastics" to get it to do what I needed.

    Thank you, oosyrag!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)