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


Groups > comp.lang.ruby > #3588

Re: File position and buffers

From jake kaiden <jakekaiden@yahoo.com>
Newsgroups comp.lang.ruby
Subject Re: File position and buffers
Date 2011-04-27 17:33 -0500
Organization Service de news de lacave.net
Message-ID <e7fe81ff36d39f75e03940dd92433130@ruby-forum.com> (permalink)
References <10d8ae57765e21626a7c64873dcba807@ruby-forum.com>

Show all headers | View raw


hi Cee -

  this may well be WAY to simple for your needs, but it seems to me you 
could so something like this:

(0text.txt is a file with 7 lines that say rubyrubyrubyetc.)

  f = "0text.txt"
  file  = File.open(f)
  buffer = []
  bufferindex = 0

  file.each_line{|line|
       buffer[bufferindex] = line.chomp
       bufferindex += 1
  }

p buffer[0]
p buffer[1]
p buffer[2]
#etc...

  of course you could also set a maximum number of lines per buffer:

  f = "0text.txt"
  file  = File.open(f)
  buffer = Hash.new{|key, value| key[value]= []}
  bufferkey = 0
  maxbuflength = 3

  file.each_line{|line|
    if buffer[bufferkey].length == maxbuflength
      bufferkey +=1
      buffer[bufferkey] << line.chomp
    else
      buffer[bufferkey] << line.chomp
    end
  }

p buffer[0]
p buffer[1]
p buffer[2]

  if the file's extremely long i guess you'd want to write a method to 
dump the buffers at some point too.

  maybe this is dumb, i hope not!
  cheers,

  -j

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

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

File position and buffers Cee Joe <cyril_jose@ymail.com> - 2011-04-27 15:02 -0500
  Re: File position and buffers Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-27 16:47 -0500
  Re: File position and buffers jake kaiden <jakekaiden@yahoo.com> - 2011-04-27 17:33 -0500
  Re: File position and buffers 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-27 19:08 -0500
  Re: File position and buffers 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-27 19:50 -0500
  Re: File position and buffers Robert Klemme <shortcutter@googlemail.com> - 2011-04-28 02:54 -0500
    Re: File position and buffers 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-28 13:06 -0500
      Re: File position and buffers 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-28 13:25 -0500
        Re: File position and buffers Cee Joe <cyril_jose@ymail.com> - 2011-04-28 13:29 -0500
  Re: File position and buffers Cee Joe <cyril_jose@ymail.com> - 2011-04-28 09:06 -0500
  Re: File position and buffers 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-28 12:47 -0500
    Re: File position and buffers Cee Joe <cyril_jose@ymail.com> - 2011-04-28 13:27 -0500
      Re: File position and buffers 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-28 18:31 -0500
        Re: File position and buffers Cee Joe <cyril_jose@ymail.com> - 2011-04-28 20:05 -0500
  Re: File position and buffers 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-28 21:58 -0500
    Re: File position and buffers Cee Joe <cyril_jose@ymail.com> - 2011-04-29 10:20 -0500
  Re: File position and buffers jake kaiden <jakekaiden@yahoo.com> - 2011-04-28 22:36 -0500
  Re: File position and buffers 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-29 12:50 -0500
    Re: File position and buffers Cee Joe <cyril_jose@ymail.com> - 2011-04-29 13:32 -0500
      Re: File position and buffers 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-29 17:45 -0500
  Re: File position and buffers jake kaiden <jakekaiden@yahoo.com> - 2011-04-29 15:38 -0500
  Re: File position and buffers Cee Joe <cyril_jose@ymail.com> - 2011-04-29 16:10 -0500

csiph-web