[twine 2.1.3] [harlowe 2.0.1] checking many strings for their contents

0 votes
asked Oct 10 by onenaa (590 points)
hello, i am thinking of using strings to store information similar to how metadata is stored.  it would just be characters representing other information so the order wouldn't matter and something cluttered and unreadable like "DmmmeFlscwaa" would work fine, but if i have several of these strings, i'm wondering what is a good way to check all their contents?

would it be good placing all the strings within an array, and then searching the array using some kind of repeating function that will first search the array's 1st, then the 2nd, and so on until it searches the entire array's length (all of the strings)?

any additional info appreciated about this also appreciated, thanks.

1 Answer

+1 vote
answered Oct 10 by greyelf (32,200 points)

You don't explain what you mean by "check all their contents", do you mean:

1. Test if the whole String value within the variable is equal to a String Literal.

(set: _var to "a value")
(if: _var is "a value")[...do something...]

2. Test if a Character withing the String value with the variable is equal to a String Literal.

(set: _var to "a value")
(if: _var's 3rd is 'v')[...do something...]

3. Something else....

Knowing this could influence any answer provided to you.

commented Oct 11 by onenaa (590 points)

well i havent got around to making it yet, but i've been thinking like this, forgive the garbagecode

 

$string1 containing data such as "amekv8nw"
$string2 containg data such as "dnfoejn2"
and so on for many such strings


with each $string contained in a single $array, and then searching this $array using a repeating code like

 

 

(live:)
[
if ($some_character) is in $array's ($counter):
[
(whatever i need to occur as a result of the $string containing the character would go here)
set $counter to it + 1
]

else:
[
(whatever i need to occur as a result of the $string NOTcontaining $some_character would go here)(stop:)
]
]


like not even two weeks ago i didn't know about arrays but they were actually simple and useful, i don't know if there is a better way to be doing this

the characters within the strings represent information about other things, each string for separate things
commented Oct 11 by greyelf (32,200 points)

note: when giving an example that contains an evaluation of a story variable it helps if you also include an assignment of that story variable.
eg. Your example contained the following:

$string1 containing data such as "amekv8nw"

... but we don't know if $string has a value of exactly "amekv8nw" in which case using an is operator to do the comparison would be the better chose, or if the value of the variable is something like "zzzzzamekv8nwzzzzz" in which case the contains operator is the correct choice.

If I understand correctly you have a collection of String values which you want to search through looking for all elements that contain a particular sub-String, and then want to do something for each of the elements found.

So assuming your collection of values and your search term look something like the following:

(set: $collection to (a: "aaazz", "bbbzz", "ccccc", "dddzz", "eeeee"))
(set: $term to "zz")

... then you can use the (find: ) macro to collate a list of the elements that contained the term you were searching for:

(set: _found to (find: _value where _value contains $term, ...$collection))

... and then use the (for: ) macro to process each of the found elements:

(for: each _value, ..._found)[
	_value
]

warning: The timer created by the (live:) macro interferes with the Reader's ability to interact with the page each time it fires, which is why that macro is not a good method for looping through a collection.

It is a good idea when first using a particular story format that you give it's manual a quick read-through so that you have a basic understanding of what functionality it supports, this will help when trying to workout how to implement some feature of your story.

...