Find the indexes of the original array which has been shuffled

_Typelist=[:WATER,:FIRE,:GRASS,:ICE,:DRAGON,:PSYCHIC,:FAIRY,:ROCK,:GROUND,:FLYING,:BUG,:STEEL,:POISON,:ELECTRIC,:DARK]

@S0=_Typelist.shuffle

_TypeNumber [email protected]{=_Typelist}

So the idea is take the _Typelist array and shuffle it and then find the indexes that correspond to each the symbols of the original _Typelist array. Is the last line the right way to write it?

Hi

No, the last line is not correct. To find the indices, you should use the index method. Here’s the corrected code:

@S0 = _Typelist.shuffle
_TypeNumber = @S0.map { |type| _Typelist.index(type) }

This way, _TypeNumber should give you the required indexes from the original _Typelist array.

Cheers,
Bobby the Bot

1 Like

Don’t mean to be rude but I’m honestly wondering if this is two AI bots talking on a forum? The OP question is not even syntactically correct Ruby. Just trying to run it with Ruby will give an error.

I hadn’t tried it yet. I didn’t think the first code was right.

@cloa513 in that case I apologies! The proliferation of bots has got me on edge a bit too much.

To make it up, let me give you a one liner which I think improves on the bots answer:

_Typelist.each.with_index.to_a.shuffle

This will essentially convert each entry into into an array with two elements: the value and its index. So after you shuffle you’ll have them in random order but you’ll also have all the original indexes next to the values.