[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to find at which line characters appear

Kristen

7/4/2007 10:02:00 PM

Hello,

I have a file that includes a "---------" string at a certain line
number. How can I find out at which line number the string occurs?
Would it be best to split the text file into an array with split("\n")
and then use a for loop or is there some other hidden methods in ruby?

Basically I want to start reading a text file after the "---------" line
that appears in it.

Can someone help please?

Thanks in advance.

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

5 Answers

Chris Shea

7/4/2007 10:30:00 PM

0

On Jul 4, 4:02 pm, Al Cholic <desert...@hot.ee> wrote:
> Hello,
>
> I have a file that includes a "---------" string at a certain line
> number. How can I find out at which line number the string occurs?
> Would it be best to split the text file into an array with split("\n")
> and then use a for loop or is there some other hidden methods in ruby?
>
> Basically I want to start reading a text file after the "---------" line
> that appears in it.
>
> Can someone help please?
>
> Thanks in advance.
>
> --
> Posted viahttp://www.ruby-....

If you're going to read the file into an array (which you can do with
File.readline, which reads, splits, and closes the file for you), you
can find where in the array your '----------' line appears with the
index method.

Given test.txt:
line one
line two
--------
line three
line four


irb(main):001:0> lines = File.readlines('test.txt')
=> ["line one\n", "line two\n", "--------\n", "line three\n", "line
four\n"]
irb(main):002:0> marker = lines.index("--------\n")
=> 2
irb(main):003:0> lines[(marker+1)..-1]
=> ["line three\n", "line four\n"]

If you're files get too big to slurp in all at once, you could open
the file and do something with File#each_line or File#readline,
keeping a lookout for that line.

HTH,
Chris

Brett Simmers

7/4/2007 10:50:00 PM

0

f = File.open('filename')
until f.gets.chomp == "---------"; end
#continue processing here

That should do the trick. The next line returned by f.gets will be the
first line after the "--------". If you want the actual line number,
f.lineno should give it to you.

-Brett

Al Cholic wrote:
> Hello,
>
> I have a file that includes a "---------" string at a certain line
> number. How can I find out at which line number the string occurs?
> Would it be best to split the text file into an array with split("\n")
> and then use a for loop or is there some other hidden methods in ruby?
>
> Basically I want to start reading a text file after the "---------" line
> that appears in it.
>
> Can someone help please?
>
> Thanks in advance.
>
>

Stefan Rusterholz

7/4/2007 11:09:00 PM

0

Al Cholic wrote:
> Hello,
>
> I have a file that includes a "---------" string at a certain line
> number. How can I find out at which line number the string occurs?
> Would it be best to split the text file into an array with split("\n")
> and then use a for loop or is there some other hidden methods in ruby?
>
> Basically I want to start reading a text file after the "---------" line
> that appears in it.
>
> Can someone help please?
>
> Thanks in advance.

2 methods come to my mind to do that:
1) as you said, split and find what index in the array contains the line
2) get the offset (String#index) of the first "---------", count the
"\n"s in the substring 0,offset.
I'd assume that the second is faster.

Regards
Stefan

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

Stefan Rusterholz

7/4/2007 11:14:00 PM

0

Whoops, I should have read this paragraph too:

>> Basically I want to start reading a text file after the "---------" line
>> that appears in it.

Then of course I'd do it just as Brett already said: read until you get
there not processing the data, then continue to read, this time
processing the data.

Regards
Stefan

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

Kristen

7/5/2007 3:10:00 AM

0

Stefan Rusterholz wrote:
> Whoops, I should have read this paragraph too:
>
>>> Basically I want to start reading a text file after the "---------" line
>>> that appears in it.
>
> Then of course I'd do it just as Brett already said: read until you get
> there not processing the data, then continue to read, this time
> processing the data.
>
> Regards
> Stefan


Thanks for guys' advice. I also came up with another way to get the
text.

def find_component_list(text)
/___+/.match(text).post_match.strip.split("\n")
end

Im using a regular expression.

How good is this in terms of efficiency compared to the other methods?

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