Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #2089 > unrolled thread

SImple question about file io

Started byPaul Joyce <pjoyce@whatsthis.ie>
First post2011-04-01 02:31 -0500
Last post2011-04-01 06:30 -0500
Articles 5 — 2 participants

Back to article view | Back to comp.lang.ruby


Contents

  SImple question about file io Paul Joyce <pjoyce@whatsthis.ie> - 2011-04-01 02:31 -0500
    Re: SImple question about file io Haruka YAGNI <hyagni@gmail.com> - 2011-04-01 02:50 -0500
    Re: SImple question about file io Haruka YAGNI <hyagni@gmail.com> - 2011-04-01 03:03 -0500
    Re: SImple question about file io Paul Joyce <pjoyce@whatsthis.ie> - 2011-04-01 05:21 -0500
      Re: SImple question about file io Haruka YAGNI <hyagni@gmail.com> - 2011-04-01 06:30 -0500

#2089 — SImple question about file io

FromPaul Joyce <pjoyce@whatsthis.ie>
Date2011-04-01 02:31 -0500
SubjectSImple question about file io
Message-ID<2f3b59003ad3466c17243f27510084bf@ruby-forum.com>
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.'

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [next] | [standalone]


#2090

FromHaruka YAGNI <hyagni@gmail.com>
Date2011-04-01 02:50 -0500
Message-ID<AANLkTi=0j7-NsyzV7ZNNOd5FGCgcaCSMryAGPTY_1Ruq@mail.gmail.com>
In reply to#2089
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.


On Fri, Apr 1, 2011 at 4:31 PM, Paul Joyce <pjoyce@whatsthis.ie> wrote:
> 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.'
>
> --
> Posted via http://www.ruby-forum.com/.
>
>



-- 
Haruka YAGNI
hyagni@gmail.com

[toc] | [prev] | [next] | [standalone]


#2091

FromHaruka YAGNI <hyagni@gmail.com>
Date2011-04-01 03:03 -0500
Message-ID<AANLkTiniSW00cATuTcDqhY2=PYus1=6KPPChaJxx7b_3@mail.gmail.com>
In reply to#2089
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


On Fri, Apr 1, 2011 at 4:31 PM, Paul Joyce <pjoyce@whatsthis.ie> wrote:
>
> #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 ## <-- This occurs a nil class error
>      curr = data[0]
>    }
> ipf.close;
> opf.close;
>
> puts 'DONE.'
>

-- 
Haruka YAGNI

[toc] | [prev] | [next] | [standalone]


#2099

FromPaul Joyce <pjoyce@whatsthis.ie>
Date2011-04-01 05:21 -0500
Message-ID<a5979caa037e322685fbc9295ee7d644@ruby-forum.com>
In reply to#2089
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

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#2100

FromHaruka YAGNI <hyagni@gmail.com>
Date2011-04-01 06:30 -0500
Message-ID<AANLkTimXgjuPmGeuDUiPjm09RJk6JUic5wSpOEd5bfK0@mail.gmail.com>
In reply to#2099
Hi Paul

On Fri, Apr 1, 2011 at 7:21 PM, Paul Joyce <pjoyce@whatsthis.ie> 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)".

-- 
Haruka YAGNI

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web