Newbie questions

Hello, I’m quite new to this whole Ruby malarky and I’m having some real
trouble with more intermediate features.
You will probally laugh at how simple this stuff is but I really can’t
figure it out. The online manuals are all written with the expert in
mind and trial and error doesn’t really work as it does with less
complex stuff.

What I’m wanting to do is read a text file and then do some stuff to it.
So…:

1: How do I use the ‘source’ command to read the file into somewhere
usable?
I’m wanting to do stuff to each line of the file individually once I
have it.

2: How do I apply a regular expression to the lines of the file?

The most I can get right now is reading the file in and printing it out.
Its just errors galore trying to figure out anything more…

I would do somethink like:

a = File.open(“name_of_file”)
whole_file = a.inject("") {|s,e| s+e}
a.close

That injects into “” each line of a, thus giving you a long string
whole_file.

However, if you are doing stuff line by line you had better do sth
like

a = File.open(“name_of_file”)
a.each do |line|
YOUR STUFF WITH line
end
a.close

For example, you want to count the number of lines where the word “foo”
appears

a = File.open(“myfile”)
count = 0
a.each do |line|
count += 1 if line =~ /foo/
end
a.close

count has now that value.

Though I am not an expert, so listen to more people.

Pedro.

On 12/11/06, Dav J. [email protected] wrote:

Hello, I’m quite new to this whole Ruby malarky and I’m having some real
trouble with more intermediate features.

malarky indeed.

What I’m wanting to do is read a text file and then do some stuff to it.
So…:

1: How do I use the ‘source’ command to read the file into somewhere
usable?
I’m wanting to do stuff to each line of the file individually once I
have it.

IO.readlines is your friend:
http://ruby-doc.org/core/classes/IO.html#M002298

2: How do I apply a regular expression to the lines of the file?

#this is grossly inefficient and totally untested, but easy to read
output_file_lines = []
IO.readlines( ‘sweetfile.txt’ ) do |line|
output_file_lines.push( line.gsub( /old|bad|stuff/, ‘New Good Stuff!’
) )
end

IO.open( ‘sweeterfile.txt’, ‘w’ ) do |file|
output_file_lines.each do |line|
file.puts( line )
end
end

hth,
-Harold

On 12/11/06, James Edward G. II [email protected] wrote:

On Dec 11, 2006, at 7:02 AM, Pedro Fortuny A. wrote:

I would do somethink like:

a = File.open(“name_of_file”)
whole_file = a.inject(“”) {|s,e| s+e}
a.close

That’s a long way to say:

whole_file = File.read(“name_of_file”)

Thanks! I kew it (and the rest) had to be suboptimal.
(… etc …)

Pedro

Thanks. I’ve got it reading in a file and checking each line against a
regular expression.

How would I go about taking individual sections of a line?
i.e. if the word ‘example:’ appears on a line then it takes that and a
few characters before or after it (it will be something specific its
just the question is in general here) so for
blah blah blah example: twenty blah blah
it returns me ‘example: twenty’

On 12/12/06, Dav J. [email protected] wrote:


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

Complete nuby here, but I’ll give it a shot:

example = “blah blah example: twenty.”
re = /example: .+[^ \t\n\r\f]/
stuff = re.match(example)
puts stuff

Output:
C:\ruby\programs>ruby regex2.rb
example: twenty.

On Dec 11, 2006, at 7:02 AM, Pedro Fortuny A. wrote:

I would do somethink like:

a = File.open(“name_of_file”)
whole_file = a.inject("") {|s,e| s+e}
a.close

That’s a long way to say:

whole_file = File.read(“name_of_file”)

However, if you are doing stuff line by line you had better do
sth like

a = File.open(“name_of_file”)
a.each do |line|
YOUR STUFF WITH line
end
a.close

File.foreach(“name_of_file”) do |line|

… use line here

end

count has now that value.

count = 0
File.foreach(“myfile”) do |line|
count += 1 if line =~ /foo/
end

James Edward G. II

Dav J. wrote:

Thanks. I’ve got it reading in a file and checking each line against a
regular expression.

How would I go about taking individual sections of a line?
i.e. if the word ‘example:’ appears on a line then it takes that and a
few characters before or after it (it will be something specific its
just the question is in general here) so for
blah blah blah example: twenty blah blah
it returns me ‘example: twenty’


#!/usr/bin/ruby -w

data = “blah blah blah blah example: twenty blah blah blah blah”

result = data.scan(%r{(example: \w+)}).flatten

puts result


output:

example: twenty

OK that makes a lot less sense…I don’t recognise many of those
commands.
How does the source file come into that?

Hi –

On Wed, 13 Dec 2006, Jason M. wrote:

it returns me ‘example: twenty’


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

Complete nuby here, but I’ll give it a shot:

example = “blah blah example: twenty.”
re = /example: .+[^ \t\n\r\f]/

You can use \S (non-whitespace) for your character class. (I do have
this nagging feeling that there was some strange edge case involving
whitespace and character classes… so test it and make sure :slight_smile:

stuff = re.match(example)
puts stuff

Output:
C:\ruby\programs>ruby regex2.rb
example: twenty.

You’ll get the right output from puts here, but you’re actually
puts’ing the whole MatchData object, not a substring of the original
string. So if you do anything string-like with it, something will go
wrong.

You can do, instead:

stuff = re.match(example)
substring = stuff[0] if stuff

at which point substring will either be the matched part of the
string, or it will be nil (thanks to the fact that “if” expressions
evaluate to nil if they’re false and have no “else”).

Another thing you can do is:

example[/example: .+\S+/]

That’s a nice handy way to grab a substring via a pattern.

David

Oh also…What if I don’t want example as part of it? Just what follows.
Would example have to be removed manually afterwards or is there a way
of getting only what follows ( ?: ???)

On 12/13/06, Dav J. [email protected] wrote:

Oh also…What if I don’t want example as part of it? Just what follows.
Would example have to be removed manually afterwards or is there a way
of getting only what follows ( ?: ???)


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

I hope I do not sound too rude, but it seems to me you have not had
access
to:

(maybe specially the “Getting Started material”).

Your problems deal with the Regexp class (and probably StringScanner,
but I
am not that sure), and files, io, etc…

Certainly, we (and especially others in this list) can answer your
questions, but the “Programming Ruby” book is the way to go at the
beginning, I think.

Pedro.

On 12/13/06, Pedro Fortuny A. [email protected] wrote:

Certainly, we (and especially others in this list) can answer your
questions, but the “Programming Ruby” book is the way to go at the
beginning, I think.

Pedro.

Pedro Fortuny A.
C/Capuchinos 14, 1. 47006 Valladolid. SPAIN
http://pfortuny.sdf-eu.org

I would actually recommend Learn ruby in 21 days over programming ruby
for a
complete newbie. It helps get a basic understanding, such that you can
better understand the pickaxe. At least, that’s how it worked for me.
/still a n00b though

One way modifying paul’s method:

data = “blah blah blah blah example: twenty blah blah blah blah”

result = data.scan(%r{(example: \w+)}).flatten
puts result
strip = result.to_s
strip.[]=(0,8, “”)
puts strip.strip

Output:
C:\ruby\programs>ruby regex3.rb
example: twenty
twenty

Another way:

data = “blah blah blah blah example: twenty blah blah blah blah”

result = data.scan(%r{(example: \w+)}).flatten
puts result
strip = result.to_s
strip.gsub!(/example: /, “”)
puts strip

Output:

C:\ruby\programs>ruby regex3.rb
example: twenty
twenty

Someone else on the list is certainly more qualified than myself to tell
you
which method is most efficient.

Dav J. wrote:

OK that makes a lot less sense…I don’t recognise many of those
commands.
How does the source file come into that?

Mr. Jones, you’ve posted in such a way that you have started a new
thread,
which breaks the original sequence of messages, and you haven’t quoted
any
of the text that you are replying to, which makes it difficult to
determine
what you are replying to. This is why no one has responded to your reply
until now.

Notice about this message that I have quoted your original message, so
you
and other readers can see what I am replying to. If you adopt this
style,
if you quote a bit of the message you are replying to, then other
readers
will be able to tell which message you are replying to, including its
author.

Usenet relies to a great extent on “threads”, that is, messages that are
connected. Usenet also relies on your putting a bit of the message that
you
are replying to in your own message, so people can put what is being
discussed into a context.