Re: wildcard usage
>got a bit of a thing that has been driving me slowly insane since I first
>started using EU. I have tried every permutation of code to create a
'>wildcard' method to substitute for many lines of branching to strings of
>data; question: is it possible to turn this...?
> --string 1
> a1={"cat","dog","mouse"}
> --string 2
> a2={"chicken","duck","cow"}
> input=x string_to_fetch="a" & x
> --where string_to_fetch is executable in the same way
> --that either string_to_fetch=a1 or string_to_fetch=a2 is
If I guess correctly, you're wanting the user to be able to specify a
variable by name. The only ways I know of to do that is to store the name as
part of the variable, or to use a parallel sequence:
strings = { { "a1", "cat","dog","mouse"}, --string 1
{ "a2", "chicken","duck","cow"} } --string 2
x = input()
string_to_fetch = {}
for c = 1 to length( strings ) do
if not compare( strings[c][1], "a" & x ) then
string_to_fetch = strings[c][2..length( strings[c] )]
exit --for loop
end if
end for
--if x was 1, then string_to_fetch would now be {"cat","dog","mouse"}
if not compare( string_to_fetch, {} ) then
--user entered number of nonexistant string
end if
Note that if you use real names (rather than a letter+number combination),
that method makes more sense, if you just need a number you could just use
the sequence subscript (after checking that it exists).
The other method:
strings = { { "cat","dog","mouse" }, --string 1
{ "chicken","duck","cow" } } --string 2
names = { "a1", "a2" }
x = input()
string_to_fetch = {}
temp_int = find( "a" & x, names )
if temp_int then
string_to_fetch = strings[temp_int]
else
--user entered number of nonexistant string
end if
--if x was 1, then string_to_fetch would now be {"cat","dog","mouse"}
Of course, this is more impressive when using real names too. ( "pets" and
"livestock" instead of "a1" and "a2", maybe...)
Either way requires storing all the variables in question in one big
sequence. This is one of the things that dynamic sequences are especially
good for.
|
Not Categorized, Please Help
|
|