From: Saeed Bhuta on
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(a)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(a)apostis:~/Ruby-Scraper$ ruby riverservicescraper.rb
riverservicescraper.rb:64: syntax error, unexpected '}', expecting kEND
}#next row
^
abcb293(a)apostis:~/Ruby-Scraper$
abcb293(a)apostis:~/Ruby-Scraper$
abcb293(a)apostis:~/Ruby-Scraper$
abcb293(a)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.
--
Posted via http://www.ruby-forum.com/.

From: Glenn Jackman on
At 2010-04-22 09:12AM, "Saeed Bhuta" wrote:
[... the code you pasted ...]
> else if i == 2

[... the error you show ...]
> riverservicescraper.rb:56: syntax error, unexpected kELSE, expecting '}'
> else if i == 2 {
> ^

Is the code you're editing different from the code you're running?
Did you forget to save in your editor?

--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous
From: Caleb Clausen on
On 4/22/10, Saeed Bhuta <saeed.bhuta(a)placr.co.uk> wrote:
> 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(a)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(a)apostis:~/Ruby-Scraper$ ruby riverservicescraper.rb
> riverservicescraper.rb:64: syntax error, unexpected '}', expecting kEND
> }#next row
> ^
> abcb293(a)apostis:~/Ruby-Scraper$
> abcb293(a)apostis:~/Ruby-Scraper$
> abcb293(a)apostis:~/Ruby-Scraper$
> abcb293(a)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..

Use the 'elsif' keyword instead of 'else if'.

From: Saeed Bhuta on
Glenn Jackman wrote:
> At 2010-04-22 09:12AM, "Saeed Bhuta" wrote:
> [... the code you pasted ...]
>> else if i == 2
>
> [... the error you show ...]
>> riverservicescraper.rb:56: syntax error, unexpected kELSE, expecting '}'
>> else if i == 2 {
>> ^
>
> Is the code you're editing different from the code you're running?
> Did you forget to save in your editor?

Apologies for the confusion, I changed the else if to elseif and now
getting the following error:

abcb293(a)apostis:~/Ruby-Scraper$ ruby riverservicescraper.rb
board divs found: 1
if statement is working for first condition
riverservicescraper.rb:56: undefined method `elseif' for
#<Object:0xb775a94c> (NoMethodError)
from riverservicescraper.rb:32:in `each'
from riverservicescraper.rb:32
from riverservicescraper.rb:22:in `each'
from riverservicescraper.rb:22
--
Posted via http://www.ruby-forum.com/.

From: Saeed Bhuta on

> Replace "else if" with "elsif", i.e.
>
> elsif i == 2
> puts "if statement is working for second condition"
> serviceDetailsHash["direction"] = directionArray[1].inner_html
> end
>
> Hope this helps,
>
> Lyle

Just spotted my mistake, I just removed the space between else if
instead of removing the 'e' too in the else. Thanks for your help Lyle.
--
Posted via http://www.ruby-forum.com/.