SImple question about file io

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

': 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.’

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-try.

Hello again, Paul

I found another problem. I am sorry not to include this in the previous
post.

As you set “opf” inside the each_line loop, “opt.puts line”(where I add
a mark)
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 = nil”) before the
loop.

Look at next examples

– code 1
flag = false

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

– result 1
1, false
2, true
3, true

– code 2
flag = false

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

– result 2
1, false
2, false
3, false

Hi Paul

On Fri, Apr 1, 2011 at 7:21 PM, Paul J. [email protected] wrote:

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 ???

As my code shows, the variables defined in a loop reset with nil on each
loop.
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)”.

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