2016-10-04 2 views
0

Ich bin neu in Ruby. Ich habe eine Reihe von Arrays mit zwei Saiten jeweils:Ruby Array: String in ganze Zahl

["[[\"Wayfair \", \"57\"]]", "[[\"Move24 \", \"26\"]]", 
    "[[\"GetYourGuide \", \"25\"]]", "[[\"Visual Meta \", \"22\"]]", 
    "[[\"FinLeap \", \"20\"]]", "[[\"Movinga \", \"20\"]]", 
    "[[\"DCMN \", \"19\"]]", ... 

Ich versuche, die Zeichenfolge mit der Anzahl der jedes Array in eine ganze Zahl zu konvertieren, aber ich habe etwas anderes, als ich erwarten:

companies = companies.map do |company| 
    c = company[0].scan(/(.+)\((\d+)\)/).inspect 
    [c[0], c[1].to_i] 
end 

Puts:

["[", 0], ["[", 0], ["[", 0], ["[", 0], ["[", 0], ["[", 0], 
    ["[", 0], ["[", 0], ["[", 0], ["[", 0], ["[", 0]] 

ich erwarte:

["Wayfair", 57], ["Move24", 26], ["GetYourGuide", 25], ... 

bitte helfen?

Voll Code:

require 'net/http' 
require 'uri' 

uri = URI('http://berlinstartupjobs.com/') #URI takes just one url 
req = Net::HTTP::Get.new(uri) #get in URI 
req['User-Agent'] = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36' #use this header 


res = Net::HTTP.start(uri.hostname, uri.port) {|http| http.request(req)} # URI documentation 

puts res.code #status code 

puts res.body 

puts res.body.scan('<a href="http://berlinstartupjobs.com/companies/') #scan in the body of the document files that match a href=... 

puts res.body.scan(/<a href="http:\/\/berlinstartupjobs\.com\/companies\/[^\s]+ class="tag-link">(.*)<\/a>/) #scan 

companies = res.body.scan(/<a href="http:\/\/berlinstartupjobs\.com\/companies\/[^\s]+ class="tag-link">(.*)<\/a>/) 


companies = companies.map do |company| 
    c = company[0].scan(/(.+)\((\d+)\)/).inspect 
    [c[0], c[1].to_i] 
end # do ... end = { } 

    puts companies.inspect 
+1

Können Sie die erwartete Ausgabe auch anzeigen? –

+1

So wie ich es sehe, haben Sie eine flache Reihe von Saiten. –

+0

Yeh, wäre schön, die erwartete Ausgabe zu sehen – Phil

Antwort

1

Ihr Code war meistens in Ordnung. Lass das .inspect am Ende fallen. Es gibt eine Zeichenfolge und kein Array zurück.

# this is what you get from the scraping. 
companies = [["Wayfair (57)"], ["Move24 (26)"], ["GetYourGuide (25)"]] 

companies = companies.flatten.map do |company| 
    c = company.scan(/(.+)\((\d+)\)/).flatten 
    [c[0], c[1].to_i] 
end 

p companies 
# >> [["Wayfair ", 57], ["Move24 ", 26], ["GetYourGuide ", 25], ...] 
+0

hey Sergio vielen Dank - das ist was ich brauchte. Ich hatte ein Gefühl, flach war die Quid und spielte damit, aber nicht so. Danke noch einmal :) – catch22

1

Sie können dies erreichen, indem Enumerable#map & unter Verwendung jedes Element Parsen mit JSON.parse:

require 'json' 

companies.map { |elem| key, val = JSON.parse(elem).flatten; [k.strip, v.to_i] } 

Statt JSON.parse Sie können auch eval verwenden, aber mit eval angesehen wird sei eine schlechte Übung.

+0

nah, seine Daten sind nicht so. OP hat nur den Druck versaut. –

1
arr = ["[[\"Wayfair \", \"57\"]]", "[[\"Move24 \", \"26\"]]"] 
result = arr.collect{|e| JSON.parse(e)[0].map{|name, value| [name.strip, value.to_i]}} 

OUTPUT: 
[[Wayfair, 57], [Move24", 26]] 
+0

nah, seine Daten sind nicht so. OP hat nur den Druck versaut. –

Verwandte Themen