1

Ruby noob here!

I have an array of structs that look like this

Token = Struct.new(:token, :ordinal)

So an array of these would look like this, in tabular form:

 Token | Ordinal
 ---------------
    C  | 2
    CC | 3
    C  | 5

And I want to group by the "token" (i.e. the left hand column) of the struct and get a count, but also preserve the "ordinal" element. So the above would look like this

 Token | Merged Ordinal | Count
 ------------------------------
    C  | 2, 5           | 2
    CC | 3              | 1

Notice that the last column is a count of the grouped tokens and the middle column merges the "ordinal". The first column ("Token") can contain a variable number of characters, and I want to group on these.

I have tried various methods, using group_by (I can get the count, but not the middle column), inject, iterating (does not seem very functional) but I just can't get it right, partly because I don't have a good grasp of Ruby and the available operations / functions.

I have also had a good look around SO, but I am not getting very far.

Any help, pointers would be much appreciated!

5
  • Can you explain the last column better? Is it the original index in the array or what? Commented Aug 3, 2012 at 23:23
  • the last column is the count of the group by - I think I mentioned that in the post - sorry if it was not clear! Commented Aug 3, 2012 at 23:47
  • I thought that you used the token to group them. Commented Aug 3, 2012 at 23:50
  • what output do you want? its unclear from your tables Commented Aug 3, 2012 at 23:54
  • I have edited my original question - hope this makes it a little clearer to everyone. Let me know if it is still vague... Commented Aug 4, 2012 at 3:15

1 Answer 1

2

Use Enumerable#group_by to do the grouping for you and use the resulting hash to get what you want with map or similar.

structs.group_by(&:token).map do |token, with_same_token|
  [token, with_same_token.map(&:ordinal), with_same_token.size]
end
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks - how would I manage the middle column - it is basically taking the Ordinal from the original elements and joining them. I have edited my question for clarity.
@RobertMS, this answer already solves your problem with the middle column. Try it and see. If you want the results to be stored in Structs rather than Arrays, you should specify that.
@Alex D, Thanks for the comment - Marc-Andre kindly edited his post with the code example so now I can see how the map / hash works - I am about to give it a try. I am not too concerned with how the results are stored, I just needed a little help with the formation of the function.
Works - thanks for your help guys. I am enjoying my first dive in to Ruby!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.