-------- Original-Nachricht --------
> Datum: Tue, 13 May 2008 01:04:09 +0900
> Von: Le Lann Jean-Christophe <jean-christophe.lelann / orange.fr>
> An: ruby-talk / ruby-lang.org
> Betreff: regexp+hash problem

> Hello !
> 
> I becoming totally mad about this simple script, whose goal is to 
> capture various information (written in a single file) about some chess 
> games : the conversion I need for the month seems to be the problem : I 
> am using a hash to convert "Feb" into 2,etc. But, this works only for 
> the first line of my file !?
> 
> The error reported is "can't convert String into Integer (TypeError)", 
> but I can't figure what it means.
> 
> Can someone help ?
> Thx
> JC
> 
> 
> 
> require 'date'
> 
> # content of the data file
> # 16: + 1181 W 1347 tihtu         [ br  2  12] B06 Res Sun Feb 17, 05:05 
> PST 2008
> # 17: - 1174 B 1242 SUPERFRIJOL   [ br  2  12] C50 Mat Sun Feb 17, 09:03 
> PST 2008
> # 18: + 1188 W 1480 kalvehale     [ br  2  12] D52 Res Sun Feb 17, 09:13 
> PST 2008
> # 19: + 1193 B 1048 borsodilaci   [ br  2  12] D00 Mat Sun Feb 17, 09:27 
> PST 2008
> # 20: + 1206 W 1439 pipharlow     [ br  2  12] A45 Res Sun Feb 17, 09:32 
> PST 2008
> # 21: - 1192 B  874 tealush       [ br  2  12] C20 Res Sun Feb 17, 09:46 
> PST 2008
> # 22: + 1194 W  861 PhatWebah     [ br  2  12] A40 Mat Sun Feb 17, 09:54 
> PST 2008
> # 23: - 1190 B 1420 Alquimista    [ br  2  12] C47 Res Sun Feb 17, 09:59 
> PST 2008
> # 24: - 1181 W 1156 jkjkjk        [ br  2  12] D06 Mat Sun Feb 17, 10:19 
> PST 2008
> # 25: - 1175 B 1266 Entangle      [ br  2  12] B40 Mat Sun Feb 17, 11:34 
> PST 2008
> 
> 
> 
> fileName=ARGV[0] if ARGV[0]
> 
> h={
>     'Jan'=>1,
>     'Feb'=>2,
>     'Mar'=>3,
>     'Apr'=>4,
>     'May'=>5,
>     'Jun'=>6,
>     'Jul'=>7,
>     'Aug'=>8,
>     'Sep'=>9,
>     'Oct'=>10,
>     'Nov'=>11,
>     'Dec'=>12,
>    }
> 
> 
> lineNumber=0
> month = h['Feb']
> File.open(fileName,'r') do |f|
>    
>    
>     f.each do |line|
>       
>         lineNumber+=1
> 
>             res=line.match(/(\d+): (\+|\-) (\d+) (W|B) (\d+) (\w+) 
> (.*\]) (\w+) (\w+) (\w+) (\w+)(\s*)(\w+), (\d+):(\d+) (\w*) (\d+)\n/)
> 
>             if res
>                 puts "\nprocessing line #{lineNumber}\n"
>                 puts line
>                 num   = res.captures[0] 
>                 score = res.captures[2].to_i
>                 m     = res.captures[10]
>                 date  = res.captures[12].to_i
>                 hour  = res.captures[13].to_i
>                 minu  = res.captures[14].to_i
>                 year  = res.captures[16].to_i
> 
>                  puts "captured month = #{m}"
>               
>                 month = h[m] # <<==  can't convert String into Integer 
> (TypeError) on the second line of the file
>               
>                 puts "#{m} corresponds to month #{month}"
> 
>                 puts num,score,m,date,hour,minu,year
> 
>                 #dateTime  = DateTime.new(y=year,m=2, d=date, h=hour, 
> min=minu, s=0)   
>             end
>     end
> end
> 
> 
> My session :
> 
> processing line 1
> 16: + 1181 W 1347 tihtu         [ br  2  12] B06 Res Sun Feb 17, 05:05 
> PST 2008
> captured month = Feb
> Feb corresponds to month 2
> 16
> 1181
> Feb
> 17
> 5
> 5
> 2008
> 
> processing line 2
> 17: - 1174 B 1242 SUPERFRIJOL   [ br  2  12] C50 Mat Sun Feb 17, 09:03 
> PST 2008
> captured month = Feb
> ChessRate.rb:59:in `[]': can't convert String into Integer (TypeError)
>         from ChessRate.rb:59
>         from ChessRate.rb:40:in `each'
>         from ChessRate.rb:40
>         from ChessRate.rb:37:in `open'
>         from ChessRate.rb:37
> 
> 
> 
> 
> 
> 
> 

Hello Jean-Christophe,

I think this is due to the fact that you have sometimes one, sometimes
two spaces in your data file between (B or W) and what follows and that
you only allow one space in between the brackets in your regexp.
Maybe it would be a good idea to set some default value for the Hash
for undefined keys so that you notice when the Regexp didn't match ...

months=Hash.new("missing") # default value of months is "missing"

Best regards,

Axel 
-- 
GMX startet ShortView.de. Hier findest Du Leute mit Deinen Interessen!
Jetzt dabei sein: http://www.shortview.de/?mc=sv_ext_mf@gmx