If-Else within a loop going through array elements

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.

On 4/22/10, Saeed B. [email protected] wrote:

detailsArray = [ ]
#direction = caption
serviceDetailsHash[“departing”] = departing
#puts departing
"ruby riverservicescraper.rb
or ‘:’ or ‘\n’ or ‘;’
abcb293@apostis:~/Ruby-Scraper$
Any help is sincerely appreciated…
Use the ‘elsif’ keyword instead of ‘else if’.

At 2010-04-22 09:12AM, “Saeed B.” 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 J. wrote:

At 2010-04-22 09:12AM, “Saeed B.” 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@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:ineach’
from riverservicescraper.rb:32
from riverservicescraper.rb:22:in `each’
from riverservicescraper.rb:22

On Thu, Apr 22, 2010 at 8:12 AM, Saeed B. [email protected]
wrote:

I’m having problems with a if-else statement within a loop which cycles
through an array, the code;

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

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

On Apr 22, 2010, at 8:29 AM, Saeed B. wrote:

#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/.

Try elsif instead of elseif

Charles J.
Vanderbilt Advanced Computing Center for Research and Education
Office: 615-343-2776
Cell: 615-478-5743

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.