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


Groups > comp.lang.ruby > #3539

pattern matching and array methods

From Mfer Dez <emphxl@yahoo.com>
Newsgroups comp.lang.ruby
Subject pattern matching and array methods
Date 2011-04-26 23:07 -0500
Organization Service de news de lacave.net
Message-ID <d4d716c8f8db27cf9490dd380e656a03@ruby-forum.com> (permalink)

Show all headers | View raw


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;

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)? What array methods
are available to me.

Thanks in advance for any information that could point me in the right
direction.

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

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


Thread

pattern matching and array methods Mfer Dez <emphxl@yahoo.com> - 2011-04-26 23:07 -0500
  Re: pattern matching and array methods Josh Cheek <josh.cheek@gmail.com> - 2011-04-26 23:27 -0500
    Re: pattern matching and array methods Josh Cheek <josh.cheek@gmail.com> - 2011-04-27 13:00 -0500
  Re: pattern matching and array methods Christopher Dicely <cmdicely@gmail.com> - 2011-04-27 00:19 -0500
  Re: pattern matching and array methods Dhruva Sagar <dhruva.sagar@gmail.com> - 2011-04-27 01:32 -0500
    Re: pattern matching and array methods Brian Candler <b.candler@pobox.com> - 2011-04-27 02:57 -0500
      Re: pattern matching and array methods Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-27 03:52 -0500
  Re: pattern matching and array methods 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-27 13:31 -0500

csiph-web