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....
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 Jones <kakaze@talk21.com> wrote:
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....
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.
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
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'
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'
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'
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 ( ?: ???)
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.
> 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
···
On 12/11/06, James Edward Gray II <james@grayproductions.net> wrote:
On Dec 11, 2006, at 7:02 AM, Pedro Fortuny Ayuso wrote:
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
> 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.
count = 0
File.foreach("myfile") do |line|
count += 1 if line =~ /foo/
end
James Edward Gray II
--
Pedro Fortuny Ayuso
C/Capuchinos 14, 1. 47006 Valladolid. SPAIN
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'
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
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
···
On Wed, 13 Dec 2006, Jason Mayer wrote:
On 12/12/06, Dav Jones <kakaze@talk21.com> wrote:
--
Q. What's a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.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, Dav Jones <kakaze@talk21.com> 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 ( ?: ???)
Someone else on the list is certainly more qualified than myself to tell you
which method is most efficient.
···
On 12/13/06, Dav Jones <kakaze@talk21.com> 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 ( ?: ???)
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
···
On 12/13/06, Pedro Fortuny Ayuso <pfortuny@gmail.com> wrote:
On 12/13/06, Dav Jones <kakaze@talk21.com> 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:
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.