feature request: get index of item in a list

acp

  • Posts: 95
I would like a way to determine which position in a list an item is.

I think the best return type of this function would be a list.

The value of each item being an index number of an item matching the input item.

For example I have a list called "FruitList" that has values of 0: apple, 1:orange, 2:banana, 3:orange

I call the function Results=FindInList("orange",Fruitlist);

Then the value of Results would be 0:1,1:3

If the requested item wasn't in the list Results would return empty

I can already do this with a custom block but I think its such a fundamental feature of list handling that it should be included in the palette.

letmethink

  • *
  • Posts: 2545
+1
I find it very useful when handling lists in other programming languages and I had presumed Stencyl had it, but seeing it doesn't, this would definitely be nice to have.
~Letmethink

Hectate

  • *
  • Posts: 4645
Extension blocks could easily be made to do this if you need it; Haxe seems to support it already (Array documentation).

Relevant for your purposes:

Quote
function filter(f:T -> Bool):Array<T>

Returns an Array containing those elements of this for which f returned true.

The individual elements are not duplicated and retain their identity.

If f is null, the result is unspecified.

Based on a quick chat in #HAXE on freenode and your example the following code should work...
Code: [Select]
var NewList = OldList.filter( function(item) return item == "orange");
Basically if the item is equal to "orange" it will appear in the new list.
:
:
Patience is a Virtue,
But Haste is my Life.
Proud member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.

Hectate

  • *
  • Posts: 4645
Oh wait, I thought I misread and then my re-read WAS a misread. Filtering is cool and all but you want the indexes of matches?
:
:
Patience is a Virtue,
But Haste is my Life.
Proud member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.

acp

  • Posts: 95
Exactly, here is the current custom block I'm using to do it right now

Input item 1 is an "any" attribute and item 2 is the list I want to find indices in

Hectate

  • *
  • Posts: 4645
Yeah that's pretty much the way to do it; most included functions for Arrays are meant to work with/return the elements, not the indices. It could be done in an extension; I believe there are List manipulation extensions already - perhaps they have a block that does this already?
:
:
Patience is a Virtue,
But Haste is my Life.
Proud member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.

Photon

  • Posts: 2693
Arrays have an "indexOf" function:

Code: [Select]
array.indexOf(value)
You can even specify an index to start from if you want to find a value and keep searching for more after it (for things like numbers and strings):

Code: [Select]
var found_list:Array<Dynamic> = new Array<Dynamic>();
var curr_index:Int;

curr_index = array.indexOf(value);
while (curr_index != -1)   // indexOf returns -1 when something is not found
{
    found_list.push(curr_index);
    curr_index = array.indexOf(value, curr_index);
}
Do NOT PM me your questions, because I likely will not respond. If I have replied to your question on the forum, keep using that topic. Thanks!

acp

  • Posts: 95
Apologies for noob question but where would I put that code to replicate the function from my last message?