S
Saeed Bhuta
Hi All,
I'm having problems with a if-else statement within a loop which cycles
through an array, the code;
"require 'rubygems'
require 'pg'
require 'hpricot'
require 'open-uri'
@doc= Hpricot(open("websiteURL"))
#Get the departure boards for both directions (eastbound and westbound)
boardsDiv = @doc.search('div[@class^="board"]')
puts "board divs found: "+boardsDiv.count.to_s
directionArray = boardsDiv.search('strong')
#puts directionArray.count.to_s
platDivArray = boardsDiv.search('table[@class^="eta"]')
#puts "Number of boards found: " +platDivArray.count.to_s
#loop through each board
platDivArray.each {|board|
trArray = board.search("tr")
#pop the dud row
trArray.delete_at(0)
detailsArray = [ ]
i = 0;
#loop through each row
trArray.each {|entry|
i+=1
tdArray = entry.search("td")
#puts "Number of td tags found: "+tdArray.count.to_s
serviceDetailsHash = Hash.new
#direction = caption
#puts direction.inner_html
route = tdArray[0].inner_html
serviceDetailsHash["route"] = route
#puts route
dueIn = tdArray[1].inner_html
serviceDetailsHash["dueIn"] = dueIn
#puts dueIn
departing = tdArray[2].inner_html
serviceDetailsHash["departing"] = departing
if i == 1
puts "if statement is working for first condition"
serviceDetailsHash["direction"] = directionArray[0].inner_html
else if i == 2
puts "if statement is working for second condition"
serviceDetailsHash["direction"] = directionArray[1].inner_html
end
#puts departing
detailsArray << serviceDetailsHash
}#next row
}# next board"
Gives the following error:
"ruby riverservicescraper.rb
riverservicescraper.rb:52: syntax error, unexpected '{', expecting kTHEN
or ':' or '\n' or ';'
riverservicescraper.rb:56: syntax error, unexpected kELSE, expecting '}'
else if i == 2 {
^
riverservicescraper.rb:56: syntax error, unexpected '{', expecting kTHEN
or ':' or '\n' or ';'
riverservicescraper.rb:60: syntax error, unexpected kEND, expecting $end
abcb293@apostis:~/Ruby-Scraper$ ruby riverservicescraper.rb
riverservicescraper.rb:52: syntax error, unexpected '{', expecting kTHEN
or ':' or '\n' or ';'
riverservicescraper.rb:56: syntax error, unexpected kELSE, expecting '}'
else if i == 2 {
^
riverservicescraper.rb:56: syntax error, unexpected '{', expecting kTHEN
or ':' or '\n' or ';'
riverservicescraper.rb:60: syntax error, unexpected kEND, expecting $end
abcb293@apostis:~/Ruby-Scraper$ ruby riverservicescraper.rb
riverservicescraper.rb:64: syntax error, unexpected '}', expecting kEND
}#next row
^
abcb293@apostis:~/Ruby-Scraper$
abcb293@apostis:~/Ruby-Scraper$
abcb293@apostis:~/Ruby-Scraper$
abcb293@apostis:~/Ruby-Scraper$ ruby riverservicescraper.rb
riverservicescraper.rb:64: syntax error, unexpected '}', expecting kEND
}#next row
^"
I know its to do with the if-else statement because when this is
commented the code runs without any errors.
Any help is sincerely appreciated..
Saeed.
I'm having problems with a if-else statement within a loop which cycles
through an array, the code;
"require 'rubygems'
require 'pg'
require 'hpricot'
require 'open-uri'
@doc= Hpricot(open("websiteURL"))
#Get the departure boards for both directions (eastbound and westbound)
boardsDiv = @doc.search('div[@class^="board"]')
puts "board divs found: "+boardsDiv.count.to_s
directionArray = boardsDiv.search('strong')
#puts directionArray.count.to_s
platDivArray = boardsDiv.search('table[@class^="eta"]')
#puts "Number of boards found: " +platDivArray.count.to_s
#loop through each board
platDivArray.each {|board|
trArray = board.search("tr")
#pop the dud row
trArray.delete_at(0)
detailsArray = [ ]
i = 0;
#loop through each row
trArray.each {|entry|
i+=1
tdArray = entry.search("td")
#puts "Number of td tags found: "+tdArray.count.to_s
serviceDetailsHash = Hash.new
#direction = caption
#puts direction.inner_html
route = tdArray[0].inner_html
serviceDetailsHash["route"] = route
#puts route
dueIn = tdArray[1].inner_html
serviceDetailsHash["dueIn"] = dueIn
#puts dueIn
departing = tdArray[2].inner_html
serviceDetailsHash["departing"] = departing
if i == 1
puts "if statement is working for first condition"
serviceDetailsHash["direction"] = directionArray[0].inner_html
else if i == 2
puts "if statement is working for second condition"
serviceDetailsHash["direction"] = directionArray[1].inner_html
end
#puts departing
detailsArray << serviceDetailsHash
}#next row
}# next board"
Gives the following error:
"ruby riverservicescraper.rb
riverservicescraper.rb:52: syntax error, unexpected '{', expecting kTHEN
or ':' or '\n' or ';'
riverservicescraper.rb:56: syntax error, unexpected kELSE, expecting '}'
else if i == 2 {
^
riverservicescraper.rb:56: syntax error, unexpected '{', expecting kTHEN
or ':' or '\n' or ';'
riverservicescraper.rb:60: syntax error, unexpected kEND, expecting $end
abcb293@apostis:~/Ruby-Scraper$ ruby riverservicescraper.rb
riverservicescraper.rb:52: syntax error, unexpected '{', expecting kTHEN
or ':' or '\n' or ';'
riverservicescraper.rb:56: syntax error, unexpected kELSE, expecting '}'
else if i == 2 {
^
riverservicescraper.rb:56: syntax error, unexpected '{', expecting kTHEN
or ':' or '\n' or ';'
riverservicescraper.rb:60: syntax error, unexpected kEND, expecting $end
abcb293@apostis:~/Ruby-Scraper$ ruby riverservicescraper.rb
riverservicescraper.rb:64: syntax error, unexpected '}', expecting kEND
}#next row
^
abcb293@apostis:~/Ruby-Scraper$
abcb293@apostis:~/Ruby-Scraper$
abcb293@apostis:~/Ruby-Scraper$
abcb293@apostis:~/Ruby-Scraper$ ruby riverservicescraper.rb
riverservicescraper.rb:64: syntax error, unexpected '}', expecting kEND
}#next row
^"
I know its to do with the if-else statement because when this is
commented the code runs without any errors.
Any help is sincerely appreciated..
Saeed.