Replace empty or strange characters within array of arrays


#1

Hi everyone:

I’m a newcomer to Ruby and started learning it approximately one-week
ago. I learn very fast (I have a photographic and logical memory) and
own a large section of books…

I wanted my first program to be a scraper as I do a lot of DB
administrative work on several websites and keep a listing of statistics

  • namely college football statistics.

I found a very good parsing idea posted on one site but it didn’t
utilize classes so I redesigned and developed it to be more modular with
what I was after…

For what I have so far - it works 99% with only one problem.

In the method for clean_celldata I’m trying to search through an array
of arrays and find one cell that contains either an empty space or the
symbol ( Â ). I want to replace that cell data with the number 120.

My problem is that I haven’t become as familiar as I need to be with
map/collect and other array functionality.

I’ve enclosed an attachment that contains the working program, class and
methods for Scraper.rb. When you test it out, you’ll see the puts data
and if you go to the bottom row (western kentucky) you’ll see there’s no
ranking number assigned. On the original website the cell is empty but
ruby processes it as the symbol ( Â ). I want to remove that and
replace it with the number 120 within the method called clean_celldata.

Many thanks in advance and I really am enjoying my first attempt at a
real object-oriented language. I’ve worked in the past with PHP,
Autoit, C++, and VB…


#2

On May 26, 2009, at 3:17 PM, Joel D. wrote:

On the original website the cell is empty but
ruby processes it as the symbol ( Â ).

Actually, on the original site, the cell is not empty, but contains a
  so you might want to test for that.

It seems like you want to assign the same value to any such row in the
“provisional” table (i.e., they all tie for the next available place).

-Rob

Rob B. http://agileconsultingllc.com
removed_email_address@domain.invalid


#3

Hi Rob,

I might just be making it more confusing for myself. In this example,
I’m really just trying to learn how to replace a part of an array with
something else when it’s an array of arrays.

In the above example, I know exactly where the issue is.

@rows[120][0]

So how to I replace whatever is in @rows[120][0] with say the number
120?


#4

Maybe I’m missing some part of the question, but you can just assign a
value
to that element in the array like anything else.
@rows[120][0] = 120

Alex


#5

Alex wrote:

Maybe I’m missing some part of the question, but you can just assign a
value
to that element in the array like anything else.
@rows[120][0] = 120

Alex

thanks Alex, the index started from 0 so it was actually @rows[119][0] =
120 but that worked.

I didn’t figure on doing the simplest form of a fix for some reason (too
much reading this week).

I still would like to know what the best way for (find/replace) in an
array would be going forward. Perhaps I’m just not searching in the
right way for the answer.

I originally tried to use a.each to look for a set condition and then
apply something else. I’m sure I’ll find the answer soon enough through
trial and error. Your fix is the simplest and works.


#6

Well, to search through your elements and do a replacement, you could do
something like this:

@rows.each do |x|
x.each do |y|
y.gsub!(/regex/) {|match| do_your_substitution(match)}
end
end

Or something to that effect, but you know what they say about regular
expressions and using them to solve problems. :slight_smile:

Using any combination of the inject/map/each methods and doing
substitutions
that way may be your best bet, but some of the more experienced rubyists
here may have better suggestions.

Alex