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

Discussion in 'Ruby' started by Saeed Bhuta, Apr 22, 2010.

  1. Saeed Bhuta

    Saeed Bhuta Guest

    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.
    --
    Posted via http://www.ruby-forum.com/.
    Saeed Bhuta, Apr 22, 2010
    #1
    1. Advertising

  2. On 4/22/10, Saeed Bhuta <> 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@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'.
    Caleb Clausen, Apr 22, 2010
    #2
    1. Advertising

  3. Saeed Bhuta

    Lyle Johnson Guest

    On Thu, Apr 22, 2010 at 8:12 AM, Saeed Bhuta <> wrot=
    e:

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


    <snip>

    > =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
    Lyle Johnson, Apr 22, 2010
    #3
  4. Saeed Bhuta

    Saeed Bhuta Guest

    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@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/.
    Saeed Bhuta, Apr 22, 2010
    #4
  5. Saeed Bhuta

    Saeed Bhuta Guest


    > 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/.
    Saeed Bhuta, Apr 22, 2010
    #5
  6. On Apr 22, 2010, at 8:29 AM, Saeed Bhuta wrote:

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

    Try elsif instead of elseif
    --
    Charles Johnson
    Vanderbilt Advanced Computing Center for Research and Education
    Office: 615-343-2776
    Cell: 615-478-5743
    Charles Johnson, Apr 22, 2010
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. diffused
    Replies:
    9
    Views:
    724
    Oscar kind
    Aug 1, 2004
  2. Replies:
    25
    Views:
    660
    MonkeeSage
    Oct 1, 2006
  3. kj
    Replies:
    15
    Views:
    551
    Lawrence D'Oliveiro
    May 23, 2009
  4. Brandon Hoppe

    for loop is not going to all array elements

    Brandon Hoppe, Mar 30, 2006, in forum: Perl Misc
    Replies:
    2
    Views:
    130
    Anno Siegel
    Mar 30, 2006
  5. Isaac Won
    Replies:
    9
    Views:
    372
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page