I have table
array${"one","two","three"."..."}
How I can get index "two"?
How I can get index of array element
-
- Posts: 475
- Joined: Fri May 15, 2015 5:15 pm
- Location: Waterville, Minnesota USA
Re: How I can get index of array element
When you need a reverse lookup for the entries in one array, you need an index table. You can make one like this:
Then you can find the index of a value by looking in the Reverse table.
Code: Select all
Reverse={}
For Key,Value In IPairs(array$) Do Reverse[Value]=Key
I'm on registered MorphOS using FlowStudio.
Re: How I can get index of array element
To add to SamuraiCrows comment.
There is no direct way to get index number, but you have to use some sort of search function (as example SamuraiCrows example) to find the item, and when you find the item, then mark up the index number.
However, if you have two different indexes both holding "two" inside them, you might end up getting either one of them, depending how you make your search and locate function.
If each item is unique, that there is always only one "two" inside the table, then you could also use two different tables:
would result in fee being printed out.
There is no direct way to get index number, but you have to use some sort of search function (as example SamuraiCrows example) to find the item, and when you find the item, then mark up the index number.
However, if you have two different indexes both holding "two" inside them, you might end up getting either one of them, depending how you make your search and locate function.
If each item is unique, that there is always only one "two" inside the table, then you could also use two different tables:
Code: Select all
TableA = { "fee", "faa", "foo"}
TableB = { ["fee"] = 1, ["faa"] = 2, ["foo"] = 3 }
indexnumber = tableB["fee"]
debugprint(TableA[indexnumber])
-
- Posts: 475
- Joined: Fri May 15, 2015 5:15 pm
- Location: Waterville, Minnesota USA
Re: How I can get index of array element
@Bugala
Don't Array indexes start at 0?
Don't Array indexes start at 0?
I'm on registered MorphOS using FlowStudio.
Re: How I can get index of array element
@SamuraiCrow
yes, mistake in my example.
yes, mistake in my example.