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


Groups > comp.lang.ruby > #3591

Re: File position and buffers

From 7stud -- <bbxx789_05ss@yahoo.com>
Newsgroups comp.lang.ruby
Subject Re: File position and buffers
Date 2011-04-27 19:08 -0500
Organization Service de news de lacave.net
Message-ID <5fcf83686c90d0c89ba3cdbb67b7255c@ruby-forum.com> (permalink)
References <10d8ae57765e21626a7c64873dcba807@ruby-forum.com>

Show all headers | View raw


Cee Joe wrote in post #995381:
> Hi all,
>
> In a bit of a rut. Have a file with a lot of text. I want to seperate
> the text in this file as entries. Each entry that I would be seperating,
> would be done so using IO.pos and when that cursor reaches a certain
> character in the file, it will ideally place all the content before that
> character into a buffer. Then the cursor will continue reading until it
> hits that same character again and put that content into a buffer, so on
> and so forth. (Character I'll be reading would be a greater than symbol)
>

There is absolutely no reason to use pos() to read that file.


>  Would I use a do iterator or use a while loop with a gets method? Or
> readlines perhaps?
>
> File:
>>entry 1
> rubyrubyrubyrubyrubyrubyrubyruby
> (newline here which I don't want)
>

chomp() removes one newline, if present, at the end of a string.

>
> PS. The file is huge, so I don't want to read it into memory. What is
> the best way to approach this? Any suggestions or comments would be
> helpful. Thanks!

Well, then you have to tell us what you want to do with the segments of 
the file.  If you store each chunk in a variable, then you will have 
read the whole file into memory.

You say your file looks like this:

>entry 1 <---WHAT'S AT THE END OF THIS LINE??
rubyrubyrubyrubyruby <---WHAT'S AT THE END OF THIS LINE??
(newline here which I don't want)

Those look like newlines.  Are you saying that your data is organized 
into paragraphs, i.e. separated by two newlines?  Like this:

>entry1\n
rubyrubyruby\n
\n
>entry2\n
rubyrubyruby\n
\n
>entry3

A paragraph is defined as two consective newlines between lines.  Note 
that in ruby the default line separator is one newline.  But you can 
change that to two newlines--or any other character:

require 'stringio'

str =<<ENDOFSTRING
>entry1
11111111111

>entry2
22222222222

>entry3
33333333333
ENDOFSTRING

input = StringIO.new(str)
$/ = "\n\n"

input.each do |para|
  p para.sub(/\n+ \z/xms, "")
end

--output:--
">entry1\n11111111111"
">entry2\n22222222222"
">entry3\n33333333333"

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