On Mon, May 12, 2008 at 11:04 AM, Le Lann Jean-Christophe
<jean-christophe.lelann / orange.fr> wrote:
> 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,
>   }

Just FYI, this is effectively the same as...

require 'date'
h = {}
Date::ABBR_MONTHNAMES.each_with_index {|obj, idx| h[obj] = idx}
h["Feb"]

...but I would tend to just use the array since the set is so small...

mons = Date::ABBR_MONTHNAMES
mons.index("Feb")

>  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

There is something else going on here, because your code, as written,
works fine for me as long as the data file is clean and I change your
regexp so as to account for extra whitespace (like in line nums 21:
and 22: -- this doesn't cause your error, it just makes you miss those
lines since res will be nil).

Todd