PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Arrays not working as I expected -
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Arrays not working as I expected -

Réponse
 
LinkBack Outils de la discussion
Vieux 20/11/2007, 15h57   #1
rrajeshh@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Arrays not working as I expected -

Hi
I am new programmer to Ruby. Just 1 days old. I do have some
background in java.

I am having a problem that is troubling me since yesterday and having
spend several hours on it, I still dont have any idea.

CODE WORKING GREAT BELOW

require 'rubygems'
require 'hpricot'
require 'open-uri'

page = Hpricot(open("http://finance.google.com/finance/historical?
q=ORCL&histperiod=weekly&start=01&num=2"))

page.search("#prices").each do |timesection|

values=timesection.search("td")
datePointValue = {
"date" => values[0].inner_html,
"open" => values[1].inner_html,
"high" => values[2].inner_html,
"low" => values[3].inner_html,
"close" => values[4].inner_html,
"volume" => values[5].inner_html
}

datePointValue.each do |key, value|
puts key + " - " + value
end
end


But when I add a loop and change the code as follows. It gives me
error message
UPDATED CODE - NOT WORKING -

page = Hpricot(open("http://finance.google.com/finance/historical?
q=ORCL&histperiod=weekly&start=01&num=1"))

page.search("#prices").each do |timesection|

timesection.search("tr").each do |datePointSets|
values = datePointSets.search("td")

datePointValue = {
"date" => values[0].inner_html,
"open" => values[1].inner_html,
"high" => values[2].inner_html,
"low" => values[3].inner_html,
"close" => values[4].inner_html,
"volume" => values[5].inner_html
}

datePointValue.each do |key, value|
puts key + " - " + value
end
end
end

ERROR MESSAGE
htest.rb:14: undefined method `inner_html' for nil:NilClass
(NoMethodError)
from htest.rb:10:in `each'
from htest.rb:10
from htest.rb:8:in `each'
from htest.rb:8

Kindly suggest.

  Réponse avec citation
Vieux 20/11/2007, 16h11   #2
Cameron McBride
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Arrays not working as I expected -

Hello,

On Nov 20, 2007 11:00 AM, <rrajeshh@gmail.com> wrote:
> Hi
> I am new programmer to Ruby. Just 1 days old. I do have some
> background in java.
>
> I am having a problem that is troubling me since yesterday and having
> spend several hours on it, I still dont have any idea.
>
> CODE WORKING GREAT BELOW
>
> require 'rubygems'
> require 'hpricot'
> require 'open-uri'
>
> page = Hpricot(open("http://finance.google.com/finance/historical?
> q=ORCL&histperiod=weekly&start=01&num=2"))
>
> page.search("#prices").each do |timesection|
>
> values=timesection.search("td")
> datePointValue = {
> "date" => values[0].inner_html,
> "open" => values[1].inner_html,
> "high" => values[2].inner_html,
> "low" => values[3].inner_html,
> "close" => values[4].inner_html,
> "volume" => values[5].inner_html
> }
>
> datePointValue.each do |key, value|
> puts key + " - " + value
> end
> end
>
>
> But when I add a loop and change the code as follows. It gives me
> error message
> UPDATED CODE - NOT WORKING -
>
> page = Hpricot(open("http://finance.google.com/finance/historical?
> q=ORCL&histperiod=weekly&start=01&num=1"))
>
> page.search("#prices").each do |timesection|
>
> timesection.search("tr").each do |datePointSets|
> values = datePointSets.search("td")
>
> datePointValue = {
> "date" => values[0].inner_html,
> "open" => values[1].inner_html,
> "high" => values[2].inner_html,
> "low" => values[3].inner_html,
> "close" => values[4].inner_html,
> "volume" => values[5].inner_html
> }
>
> datePointValue.each do |key, value|
> puts key + " - " + value
> end
> end
> end
>
> ERROR MESSAGE
> htest.rb:14: undefined method `inner_html' for nil:NilClass
> (NoMethodError)
> from htest.rb:10:in `each'
> from htest.rb:10
> from htest.rb:8:in `each'
> from htest.rb:8
>
> Kindly suggest.


At a knee-jerk guess, you've changed how 'values' is populated. I'd
wager that the values result from the Hpricot search isn't properly
set. You should print values before you set the datePointValue hash
and see what it contains.

In addition, you might consider another sanity check if you explicitly
access 6 elements of an array. Like

raise "Parsing Error: values not set correctly" if (values.nil? or
values.size < 6)

Cheers.

Cameron

  Réponse avec citation
Vieux 20/11/2007, 16h40   #3
rrajeshh@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Arrays not working as I expected -

On Nov 20, 11:11 am, Cameron McBride <cameron.mcbr...@gmail.com>
wrote:
> Hello,
>
> On Nov 20, 2007 11:00 AM, <rraje...@gmail.com> wrote:
>
>
>
>
>
> > Hi
> > I am new programmer to Ruby. Just 1 days old. I do have some
> > background in java.

>
> > I am having a problem that is troubling me since yesterday and having
> > spend several hours on it, I still dont have any idea.

>
> > CODE WORKING GREAT BELOW

>
> > require 'rubygems'
> > require 'hpricot'
> > require 'open-uri'

>
> > page = Hpricot(open("http://finance.google.com/finance/historical?
> > q=ORCL&histperiod=weekly&start=01&num=2"))

>
> > page.search("#prices").each do |timesection|

>
> > values=timesection.search("td")
> > datePointValue = {
> > "date" => values[0].inner_html,
> > "open" => values[1].inner_html,
> > "high" => values[2].inner_html,
> > "low" => values[3].inner_html,
> > "close" => values[4].inner_html,
> > "volume" => values[5].inner_html
> > }

>
> > datePointValue.each do |key, value|
> > puts key + " - " + value
> > end
> > end

>
> > But when I add a loop and change the code as follows. It gives me
> > error message
> > UPDATED CODE - NOT WORKING -

>
> > page = Hpricot(open("http://finance.google.com/finance/historical?
> > q=ORCL&histperiod=weekly&start=01&num=1"))

>
> > page.search("#prices").each do |timesection|

>
> > timesection.search("tr").each do |datePointSets|
> > values = datePointSets.search("td")

>
> > datePointValue = {
> > "date" => values[0].inner_html,
> > "open" => values[1].inner_html,
> > "high" => values[2].inner_html,
> > "low" => values[3].inner_html,
> > "close" => values[4].inner_html,
> > "volume" => values[5].inner_html
> > }

>
> > datePointValue.each do |key, value|
> > puts key + " - " + value
> > end
> > end
> > end

>
> > ERROR MESSAGE
> > htest.rb:14: undefined method `inner_html' for nil:NilClass
> > (NoMethodError)
> > from htest.rb:10:in `each'
> > from htest.rb:10
> > from htest.rb:8:in `each'
> > from htest.rb:8

>
> > Kindly suggest.

>
> At a knee-jerk guess, you've changed how 'values' is populated. I'd
> wager that the values result from the Hpricot search isn't properly
> set. You should print values before you set the datePointValue hash
> and see what it contains.
>
> In addition, you might consider another sanity check if you explicitly
> access 6 elements of an array. Like
>
> raise "Parsing Error: values not set correctly" if (values.nil? or
> values.size < 6)
>
> Cheers.
>
> Cameron- Hide quoted text -
>
> - Show quoted text -


Thats is exactly what i thought so I printed the values as shown
below. this worked fine....

page = Hpricot(open("http://finance.google.com/finance/historical?
q=ORCL&histperiod=weekly&start=01&num=1"))

page.search("#prices").each do |timesection|

timesection.search("tr").each do |datePointSets|
values = datePointSets.search("td")

values.length.times do |i|
puts values[i].inner_html # WORKS GREAT!! Prints 5 values
end
end
end

Interestingly the loop prints the values correctly but when access
these as shown below I get the same error.
page = Hpricot(open("http://finance.google.com/finance/historical?
q=ORCL&histperiod=weekly&start=01&num=1"))

page.search("#prices").each do |timesection|

timesection.search("tr").each do |datePointSets|
values = datePointSets.search("td")

puts values[0].inner_html # ERRORS HERE
puts values[1].inner_html # ERRORS HERE

end
end
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h32.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14213 seconds with 11 queries