[Newbie Alert] Probably missing something simple here ... help {wimper}

Discussion in 'Ruby' started by B. Angell, Aug 16, 2005.

  1. B. Angell

    B. Angell Guest

    Am trying to read from a file and write to a number of files based on
    the first 4 letters of the data line. Therefore, I want to be able to
    write the line: A0037775830|lkajsdlkfjsaljf;lsakjfdsa;jf to file A003
    *and* append all of the A003 lines as well. Here is the hack I have
    below, however, produces errors and I know I am missing something
    simple/easy ..... as follows:

    #!/usr/bin/ruby -w
    File.open("meshdata.txt") do |file|
    while line = file.gets
    a = line
    b = a[0,4]
    File.open(b,"w") do |afile|
    puts a.afile

    end
    end
    end

    Any/all help in this matter is greatly appreciated.

    -Bob-
     
    B. Angell, Aug 16, 2005
    #1
    1. Advertising

  2. B. Angell

    Ben Giddings Guest

    On Tuesday 16 August 2005 16:42, B. Angell wrote:
    > Am trying to read from a file and write to a number of files based on
    > the first 4 letters of the data line. Therefore, I want to be able to
    > write the line: A0037775830|lkajsdlkfjsaljf;lsakjfdsa;jf to file A003
    > *and* append all of the A003 lines as well. Here is the hack I have
    > below, however, produces errors and I know I am missing something
    > simple/easy ..... as follows:
    >
    > #!/usr/bin/ruby -w
    > File.open("meshdata.txt") do |file|
    > while line = file.gets
    > a = line
    > b = a[0,4]
    > File.open(b,"w") do |afile|
    > puts a.afile


    afile.puts a

    > end
    > end
    > end


    Ben
     
    Ben Giddings, Aug 16, 2005
    #2
    1. Advertising

  3. Re: Probably missing something simple here ... help {wimper}

    B. Angell wrote:
    > Am trying to read from a file and write to a number of files based on
    > the first 4 letters of the data line. Therefore, I want to be able to
    > write the line: A0037775830|lkajsdlkfjsaljf;lsakjfdsa;jf to file A003
    > *and* append all of the A003 lines as well. Here is the hack I have
    > below, however, produces errors and I know I am missing something
    > simple/easy ..... as follows:
    >
    > #!/usr/bin/ruby -w
    > File.open("meshdata.txt") do |file|
    > while line = file.gets
    > a = line
    > b = a[0,4]
    > File.open(b,"w") do |afile|
    > puts a.afile
    >
    > end
    > end
    > end


    You are repeatedly opening a file that is already open.
    Each time you do that, you lose what you have already written.

    outfilename = ""; outfile = nil
    File.open("meshdata.txt") do |file|
    while line = file.gets
    b = line[0,4]
    if b != outfilename
    outfile.close if outfile
    outfilename = b
    outfile = File.open(outfilename,"w")
    end
    outfile.puts line
    end
    end
    outfile.close if outfile
     
    William James, Aug 16, 2005
    #3
  4. On 8/16/05, B. Angell <> wrote:
    > Am trying to read from a file and write to a number of files based on
    > the first 4 letters of the data line. Therefore, I want to be able to
    > write the line: A0037775830|lkajsdlkfjsaljf;lsakjfdsa;jf to file A003
    > *and* append all of the A003 lines as well. Here is the hack I have
    > below, however, produces errors and I know I am missing something
    > simple/easy ..... as follows:
    >=20
    > #!/usr/bin/ruby -w
    > File.open("meshdata.txt") do |file|
    > while line =3D file.gets
    > a =3D line
    > b =3D a[0,4]
    > File.open(b,"w") do |afile|
    > puts a.afile
    >=20
    > end
    > end
    > end


    I'm at the end of a long day here, and I haven't bothered to try to
    understand your goals/requirements/etc, so forgive me if this is just
    a senseless babble. However, here's what comes to mind:

    #!/usr/bin/ruby -w
    File.open("meshdata.txt").each_line do |line|
    thingy =3D line[0,4]
    File.open(thingy,"a+") do |new_file|
    new_file.puts line
    end
    end

    If your input file is hooooj, you would probably want to sort it
    first, to minimize the number of times you open and close files.
     
    Wilson Bilkovich, Aug 16, 2005
    #4
  5. Re: Probably missing something simple here ... help {wimper}

    Wilson Bilkovich wrote:
    > On 8/16/05, B. Angell <> wrote:
    > > Am trying to read from a file and write to a number of files based on
    > > the first 4 letters of the data line. Therefore, I want to be able to
    > > write the line: A0037775830|lkajsdlkfjsaljf;lsakjfdsa;jf to file A003
    > > *and* append all of the A003 lines as well. Here is the hack I have
    > > below, however, produces errors and I know I am missing something
    > > simple/easy ..... as follows:
    > >
    > > #!/usr/bin/ruby -w
    > > File.open("meshdata.txt") do |file|
    > > while line = file.gets
    > > a = line
    > > b = a[0,4]
    > > File.open(b,"w") do |afile|
    > > puts a.afile
    > >
    > > end
    > > end
    > > end

    >
    > I'm at the end of a long day here, and I haven't bothered to try to
    > understand your goals/requirements/etc, so forgive me if this is just
    > a senseless babble. However, here's what comes to mind:
    >
    > #!/usr/bin/ruby -w
    > File.open("meshdata.txt").each_line do |line|
    > thingy = line[0,4]
    > File.open(thingy,"a+") do |new_file|
    > new_file.puts line
    > end
    > end
    >
    > If your input file is hooooj, you would probably want to sort it
    > first, to minimize the number of times you open and close files.


    Doesn't this open and close a file every time a line is
    read from meshdata.txt, even if that file is sorted?
     
    William James, Aug 16, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JezB

    Am I missing something here ??

    JezB, Dec 4, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    325
  2. =?Utf-8?B?RGVscGhpbg==?=

    Am I missing something here?

    =?Utf-8?B?RGVscGhpbg==?=, Sep 8, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    446
    S. Justin Gengo
    Sep 8, 2005
  3. python newbie
    Replies:
    0
    Views:
    314
    python newbie
    May 9, 2004
  4. Paul
    Replies:
    4
    Views:
    328
  5. Replies:
    4
    Views:
    139
    Brian Candler
    Feb 19, 2009
Loading...

Share This Page