If-Else within a loop going through array elements...

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.
 
C

Caleb Clausen

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

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

Lyle Johnson

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

=A0 =A0if i =3D=3D 1
=A0 =A0 =A0puts "if statement is working for first condition"
=A0 =A0 =A0serviceDetailsHash["direction"] =3D directionArray[0].inner_ht= ml

=A0 =A0else if i =3D=3D 2
=A0 =A0 =A0puts "if statement is working for second condition"
=A0 =A0 =A0serviceDetailsHash["direction"] =3D directionArray[1].inner_ht= ml
=A0 =A0end

Replace "else if" with "elsif", i.e.

elsif i =3D=3D 2
puts "if statement is working for second condition"
serviceDetailsHash["direction"] =3D directionArray[1].inner_html
end

Hope this helps,

Lyle
 
S

Saeed Bhuta

Glenn said:
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@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
 
S

Saeed Bhuta

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.
 
C

Charles Johnson

Glenn said:
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@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
Try elsif instead of elseif
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top