Go Back   CodingForums.com > :: Server side development > Ruby & Ruby On Rails

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-27-2011, 02:14 AM   PM User | #1
send
New to the CF scene

 
Join Date: Apr 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
send is an unknown quantity at this point
Pattern matching and array methods

I have a text file that is structured like so:

1:1 abcdefg
1:2 abcdefg
1:3 abcdefg
1:4 abcdefg
1:5 abcdefg

I would like to be able to print out a subset of the file ie: print the line beginning with 1:2 through the line beginning with 1:4

So far, I've started with this;

Code:
lines = File.readlines("file.txt")
This puts each line of the text file into an array, so the lines[] array looks like this:

line[0] is 1:1 abcdefg
line[1] is 1:2 abcdefg
line[2] is 1:3 abcdefg
etc.

If I want to print out the lines that start with 1:2 through 1:4, how should I proceed? Some of the text files won't be "aligned" in that line[0] won't always be 1:1. If a user would like to print the lines containing 3:9 - 3:31, how can I scan each line of the array and pattern match the boundaries (3:9 - 3:31 in this example)?

Thanks in advance for any information that could point me in the right direction.
send is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:07 AM.


Advertisement
Log in to turn off these ads.