Thanks so much for your help. I'm reading O'Reilly Ruby Cookbook, which
I have found rather difficult to learn from. I'm also reading a free
tutorial online at rubylearning.com. Neither provide enough
clarification for me as I am new to Ruby.
Given your previous posting on procs and the code you submitted here,
I got the sense that you were jumping to some advanced topics before
having a good sense for the basics. And that's why I asked about the
resources you were using; something didn't seem quite right.
A cookbook is designed to give you solutions to real-world, practical,
common problems, and the authors are not obligated to cover the
basics. So for most people, they aren't the best resource to learn
the language (but they can be invaluable at a later stage). And I
don't know much about that web site.
What languages do you already know?
Also, I'm very confused on the regular expressions:
Problem: I want to have a file that has information, for instance,
Version #, Part #
v.1.0-A, 000000
v.1.0-A, 000000
Now I want to store each character that's not a 'v', '.', or '-' as one
string in one array called version (so element one should read "10A").
Then I want to say, "Ok I'm at a comma now, so I want to store the
following information in the next array"... so I want to store the whole
part# as one element of an array, called part (element one should read
"000000"). Then... I want to say, "Ok, I'm at a newline, so start the
same process again"
basically, i want to compare if two lines have the same version# or
part#
so in this case, i want to check if version[0] == version[1], which
should return true. ("10A"=="10A")
Probably a very confusing explanation, but if you can help that'd be
great. I'm trying to learn by doing some examples like this, but it's
very difficult =(
Parsing your file character by character would be a very complicated
way to do it. There are two common ways to approach it. Both involve
reading the data line by line (rather than character by character)
since a line equates to a record for you.
The first way would be to use String#split to break apart the version
and part numbers and then to use String#gsub to remove the unwanted
characters.
The second way would be to use a regular expression with grouping
parentheses to pull out the desired data. You could do a String#gsub
to further process a piece of data to get rid of unwanted characters,
although it's unclear to me why it's more desirable to have "10A"
rather than "1.0-A".
Also, if you want to look for duplicates, storing the data in an Array
will likely lead to O(n**2) operation. If you have lots of data, that
can be inefficient. You may want to consider the Set class in the
'set' library, or alternatively using one or two Hashes keyed on the
data you want to check for duplicates.
Eric
====
LearnRuby.com offers Rails & Ruby HANDS-ON public & ON-SITE
workshops.
Ruby Fundamentals Wkshp June 16-18 Ann Arbor, Mich.
Ready for Rails Ruby Wkshp June 23-24 Ann Arbor, Mich.
Ruby on Rails Wkshp June 25-27 Ann Arbor, Mich.
Ruby Plus Rails Combo Wkshp June 23-27 Ann Arbor, Mich
Please visit
http://LearnRuby.com for all the details.