# Replace empty or strange characters within array of arrays

**URL:** <https://www.ruby-forum.com/t/replace-empty-or-strange-characters-within-array-of-arrays/168866>\
**Category:** Ruby\
**Created:** [May 26, 2009, 9:17pm UTC](https://www.ruby-forum.com/t/replace-empty-or-strange-characters-within-array-of-arrays/168866 "2009-05-26T21:17:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [May 26, 2009, 9:17pm UTC](https://www.ruby-forum.com/t/replace-empty-or-strange-characters-within-array-of-arrays/168866/1 "2009-05-26T21:17:43Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [May 26, 2009, 9:41pm UTC](https://www.ruby-forum.com/t/replace-empty-or-strange-characters-within-array-of-arrays/168866/2 "2009-05-26T21:41:31Z")

</div>

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  
&nbsp; 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](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [May 26, 2009, 9:46pm UTC](https://www.ruby-forum.com/t/replace-empty-or-strange-characters-within-array-of-arrays/168866/3 "2009-05-26T21:46:10Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [May 26, 2009, 9:58pm UTC](https://www.ruby-forum.com/t/replace-empty-or-strange-characters-within-array-of-arrays/168866/4 "2009-05-26T21:58:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [May 26, 2009, 10:06pm UTC](https://www.ruby-forum.com/t/replace-empty-or-strange-characters-within-array-of-arrays/168866/5 "2009-05-26T22:06:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [May 26, 2009, 10:17pm UTC](https://www.ruby-forum.com/t/replace-empty-or-strange-characters-within-array-of-arrays/168866/6 "2009-05-26T22:17:56Z")

</div>

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. 🙂

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
