1

I have this query in Ruby to fetch some data from the database:

@mastertest = connection.execute("select code_ver, date from mastertest")

So now I assume @mastertest is a 2-dimensional array containing all rows and columns

I try this to print everything out:

@mastertest.each do |row|
  puts row[0]
end

And it prints the first column of all rows.

Now I want to find out the unique values from the first column of all rows and count the number of it's occurrence too. I know it can be done through Hash or Array. Can someone help me with this?

0

1 Answer 1

1
result = @mastertest.map{ |row| row[0] }.uniq
result.size #=> number of occurrences
Sign up to request clarification or add additional context in comments.

4 Comments

I want to assign the result to another 2d array to print the results....so should i do something like this: @temp = @mastertest.map{ |row| row[0] }.uniq.size ???
Is result an array here? Because I am not getting the result I intend to get. To be more clear, the first row in my 2D-array will have data like this : 111,222,222,333,111 So my output should be a 2d array such that result=[ {111,2}, {222,2}, {333,1} ]
Try to print to understand what kind of data you have "{ |row| puts row; row[0]}"
I am quite new to ruby on rails...and when I make any changes in my controller, I cannot see the output being printed when i start my server.....I am testing my results by just making changes in my view....can you help me out here?

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.