0

I'm trying to extract data from database and use it with a pie-chart for example.

I was able to extract data from the column "browser" from the database which is a string and has this format : "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0"

When I use the method "count" in my query, data will be saved as a hash like this :

{"Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0"=>5, "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:51.0) Gecko/20100101 Firefox/51.0"=>9}

I'd like to show only the version of the browser or the OS for example. Is there any method (split or substring) to have a hash like this ?? and of course the method "count" should be working

{"Firefox/45.0"=>5, "Firefox/51.0"=>9}

I tried to use split but when using the method count, there will be no data found since it's saved in the database as a whole string "Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0". there is no "Firefox/45.0" in the column browser in my table in the database.

I tried also to use substring in mysql query when using a simple query from console I get the results I want but since I'm using Ruby on Rails and ActiveRecords the "substring" is not returning the same result (not working properly)

Any help please?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
media
  • 135
  • 1
  • 7

1 Answers1

0

Do the following:

input = {"Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Firefox/45.0"=>5, "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:51.0) Gecko/20100101 Firefox/51.0"=>9}
result = {}
input.each { |k, v| result[k.split.last] = v }

Replace the input with your actual query.

What it does is that it:

  • iterates on each element of the input
  • For each one, use the key as k and the value as v,
  • Take k, split it (cut the string into words separated by spaces), and keep the last word using last
  • Add to result a new entry where the key is the last word selected, and the value is v.
yoones
  • 2,394
  • 1
  • 16
  • 20
  • thank you @yoones, it hepled me but could you please explain to me what the `{ |k, v| result[k.split.last] = v }` did exactly so I can do more things :) – media May 02 '17 at 16:08
  • @media I added some explanation, check it out. – yoones May 02 '17 at 16:14
  • @yoones NB: I know I can use `split(' ')[3]` instead of last for example but that will returns only 'Linux' and not 'Linux x86_64 right ? any idea about that? – media May 02 '17 at 16:22
  • @media try this: `"toto titi tata plouf".split[1..2].join(' ')` – yoones May 02 '17 at 16:25
  • I think this answer is correct, but it could be cleaner. You don't need to create an empty hash and then fill it up; instead just do `input.transform_keys { |browser| browser.split.last }`. – moveson May 02 '17 at 16:31
  • @moveson `transform_keys` can be used but there are two things to consider first: is he working on an activerecord result or a hash? and will he still need the original data afterwards? Since I don't know, I go with the most generic version. – yoones May 02 '17 at 16:37