SImple question about file io

P

Paul Joyce

Hello there

I am a little frustrated by a simple problem. This the script below is
failing with

C:/projects/LAF-01/For-District-Managers/breakout.rb:23:in `block in
<main>': wrong argument type Object (expected Data) (TypeError)

This script simple breaks a large file into smaller ones byu the first
column (District). However, it will not write to the file opf for some
reason. I have done this so many times - what am Imissing.

thanks
p


data = Array.new
curr = "x"
colhead = "District,Contract No,Contract Status,Tariff,Name,Supply Point
No,Meter No,Meter Make,Address 1,Address 2,Address 3,Address 4,ICS
Feeder Information,Feeder Name,Feeder Meter No,Comments"

#puts template
ipf = File.new("All-Contracts-29-Mar-11.csv","r")
ipf.each_line { |line|
next if line.match(/^$/)
data = line.split(",")
if curr == "x" # Initialize
puts "Initialize . . " + data[0]
opf = File.new(data[0] + '-feeder-customer.csv',"r+")
opf.puts colhead # <----- HERE
end
if curr != data[0] and curr != "x"
puts "New File . . . " + data[0]
opf.close
opf = File.new(data[0] + "-feeder-customer.csv","r+")
opf.puts colhead
end
opf.puts line
curr = data[0]
}
ipf.close;
opf.close;

puts 'DONE.'
 
H

Haruka YAGNI

Hi. Paul.

I could not reproduce your error, but there is a obvious problem.
File open mode "r+" does not create a file if it is not existing.
This might not be what you want.

Please replace "r+" with "a" (add new lines to the last of a file) and re-t=
ry.


Hello there

I am a little frustrated by a simple problem. =A0This the script below is
failing with

C:/projects/LAF-01/For-District-Managers/breakout.rb:23:in `block in
<main>': wrong argument type Object (expected Data) (TypeError)

This script simple breaks a large file into smaller ones byu the first
column (District). =A0However, it will not write to the file opf for some
reason. =A0I have done this so many times - what am Imissing.

thanks
p


data =3D Array.new
curr =3D "x"
colhead =3D "District,Contract No,Contract Status,Tariff,Name,Supply Poin= t
No,Meter No,Meter Make,Address 1,Address 2,Address 3,Address 4,ICS
Feeder Information,Feeder Name,Feeder Meter No,Comments"

#puts template
ipf =3D File.new("All-Contracts-29-Mar-11.csv","r")
=A0 =A0 =A0ipf.each_line { |line|
=A0 =A0 =A0next if line.match(/^$/)
=A0 =A0 =A0data =3D line.split(",")
=A0 =A0 =A0if curr =3D=3D "x" =A0 =A0 # =A0Initialize
=A0 =A0 =A0 =A0puts "Initialize . . " + data[0]
=A0 =A0 =A0 =A0opf =3D File.new(data[0] + '-feeder-customer.csv',"r+")
=A0 =A0 =A0 =A0opf.puts colhead =A0 # <----- =A0HERE
=A0 =A0 =A0end
=A0 =A0 =A0if curr !=3D data[0] and curr !=3D "x"
=A0 =A0 =A0 =A0 puts "New File . . . " + data[0]
=A0 =A0 =A0 =A0 opf.close
=A0 =A0 =A0 =A0 opf =3D File.new(data[0] + "-feeder-customer.csv","r+")
=A0 =A0 =A0 =A0 opf.puts colhead
=A0 =A0 =A0end
=A0 =A0 =A0opf.puts line
=A0 =A0 =A0curr =3D data[0]
=A0 =A0}
ipf.close;
opf.close;

puts 'DONE.'



--=20
Haruka YAGNI
(e-mail address removed)
 
H

Haruka YAGNI

Hello again, Paul

I found another problem. I am sorry not to include this in the previous po=
st.

As you set "opf" inside the each_line loop, "opt.puts line"(where I add a m=
ark)
won't work when both contents of ifs are not processed, i.e. the opf
is not defined
inside the loop.

To solve this problem, add definition of opf ("opf =3D nil") before the loo=
p.


Look at next examples

-- code 1
flag =3D false

1.upto(3) do |i|
unless flag
bad_var =3D 1000
flag =3D true
end
puts "#{i}, #{bad_var.nil?}"
end

-- result 1
1, false
2, true
3, true

-- code 2
flag =3D false

bad_var =3D nil # << bad_var is defined here
1.upto(3) do |i|
unless flag
bad_var =3D 1000
flag =3D true
end
puts "#{i}, #{bad_var.nil?}"
end

-- result 2
1, false
2, false
3, false


#puts template
ipf =3D File.new("All-Contracts-29-Mar-11.csv","r")
=A0 =A0 =A0ipf.each_line { |line|
=A0 =A0 =A0next if line.match(/^$/)
=A0 =A0 =A0data =3D line.split(",")
=A0 =A0 =A0if curr =3D=3D "x" =A0 =A0 # =A0Initialize
=A0 =A0 =A0 =A0puts "Initialize . . " + data[0]
=A0 =A0 =A0 =A0opf =3D File.new(data[0] + '-feeder-customer.csv',"r+")
=A0 =A0 =A0 =A0opf.puts colhead =A0 # <----- =A0HERE
=A0 =A0 =A0end
=A0 =A0 =A0if curr !=3D data[0] and curr !=3D "x"
=A0 =A0 =A0 =A0 puts "New File . . . " + data[0]
=A0 =A0 =A0 =A0 opf.close
=A0 =A0 =A0 =A0 opf =3D File.new(data[0] + "-feeder-customer.csv","r+")
=A0 =A0 =A0 =A0 opf.puts colhead
=A0 =A0 =A0end
=A0 =A0 =A0opf.puts line ## <-- This occurs a nil class error
=A0 =A0 =A0curr =3D data[0]
=A0 =A0}
ipf.close;
opf.close;

puts 'DONE.'

--=20
Haruka YAGNI
 
P

Paul Joyce

Hi Haruka

Thank you for you help. I guess its a good idea to initialise vars.
Adding opf = nil solves the problem, though I must confess to not being
entirely sure why, since the initialisation should create it, and as far
as I can tell it exists for the rest of the runtime, just closes and
re-opens pointing to a different file. I mean the problem arises in the
line following the File.new statement that defines opf ???

Anyway - it now works - thats the main thing!

thanks again
much appreciated
Paul
 
H

Haruka YAGNI

Hi Paul

Adding opf =3D nil solves the problem, though I must confess to not being
entirely sure why, since the initialisation should create it, and as far
as I can tell it exists for the rest of the runtime, just closes and
re-opens pointing to a different file. =A0I mean the problem arises in th= e
line following the File.new statement that defines opf ???

As my code shows, the variables defined in a loop reset with nil on each lo=
op.
This is a feature of "local-variable(in-block)".

When a variable is defined outside the loop, it is "local-variable"
(not in-block).
This local-variable is not reseted and works as you expect.

To check the type of a variable 'v', try "p defined?(v)".

--=20
Haruka YAGNI
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top