|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 ? 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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
Why don't use Date.strptime to match the date info ?
On Mon, May 12, 2008 at 4:04 PM, 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 ? > 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 > > > > > > > > > -- Go outside! The graphics are amazing! |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Le Lann Jean-Christophe 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 ? > 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 fileName = "data.txt" 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, } puts h["Feb"] h = [1, 2, 3] puts h["Feb"] --output:-- 2 r1test.rb:21:in `[]': cannot convert String into Integer (TypeError) from r1test.rb:21 So, I would guess that you didn't actually post the code that produced that error message. When I run the code you posted, this is the output: 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 before res in res processing line 2 # 17: - 1174 B 1242 SUPERFRIJOL [ br 2 12] C50 Mat Sun Feb 17, 09:03 PST 2008 captured month = Feb Feb corresponds to month 2 17 1174 Feb 17 9 3 2008 before res in res processing line 3 # 18: + 1188 W 1480 kalvehale [ br 2 12] D52 Res Sun Feb 17, 09:13 PST 2008 captured month = Feb Feb corresponds to month 2 18 1188 Feb 17 9 13 2008 before res in res processing line 4 # 19: + 1193 B 1048 borsodilaci [ br 2 12] D00 Mat Sun Feb 17, 09:27 PST 2008 captured month = Feb Feb corresponds to month 2 19 1193 Feb 17 9 27 2008 before res in res processing line 5 # 20: + 1206 W 1439 pipharlow [ br 2 12] A45 Res Sun Feb 17, 09:32 PST 2008 captured month = Feb Feb corresponds to month 2 20 1206 Feb 17 9 32 2008 before res before res before res in res processing line 8 # 23: - 1190 B 1420 Alquimista [ br 2 12] C47 Res Sun Feb 17, 09:59 PST 2008 captured month = Feb Feb corresponds to month 2 23 1190 Feb 17 9 59 2008 before res in res processing line 9 # 24: - 1181 W 1156 jkjkjk [ br 2 12] D06 Mat Sun Feb 17, 10:19 PST 2008 captured month = Feb Feb corresponds to month 2 24 1181 Feb 17 10 19 2008 before res in res processing line 10 # 25: - 1175 B 1266 Entangle [ br 2 12] B40 Mat Sun Feb 17, 11:34 PST 2008 captured month = Feb Feb corresponds to month 2 25 1175 Feb 17 11 34 2008 -- Posted via http://www.ruby-forum.com/. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
-------- 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 ? > 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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 ? > 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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Sandro Paganotti wrote:
> Why don't use Date.strptime to match the date info ? > > > > On Mon, May 12, 2008 at 4:04 PM, Le Lann Jean-Christophe Or leave the hard work to Date.parse: require 'date' DATA.each do |line| d = Date.parse(line) puts "#{d.day} #{d.month} #{d.year}" end #=> # 12 1 2008 # 17 2 2008 # 17 7 2008 # 17 3 2008 # 29 2 2008 # 17 12 2008 # 17 5 2008 __END__ 16: + 1181 W 1347 tihtu [ br 2 12] B06 Res Sun Jan 12, 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 Jul 17, 09:13 PST 2008 19: + 1193 B 1048 borsodilaci [ br 2 12] D00 Mat Sun Mar 17, 09:27 PST 2008 20: + 1206 W 1439 pipharlow [ br 2 12] A45 Res Sun Feb 29, 09:32 PST 2008 21: - 1192 B 874 tealush [ br 2 12] C20 Res Sun Dec 17, 09:46 PST 2008 22: + 1194 W 861 PhatWebah [ br 2 12] A40 Mat Sun May 17, 09:54 PST 2008 <end code> Regards, Siep -- Posted via http://www.ruby-forum.com/. |
|
![]() |
| Outils de la discussion | |
|
|