Forum: Ruby Replace empty or strange characters within array of arrays

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B6a2e2c1454c56eb686c5a38b5f73d02?d=identicon&s=25 Joel Dezenzio (elricstorm)
on 2009-05-26 21:17
Attachment: scraper.rb (3 KB)
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..
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2009-05-26 21:41
(Received via mailing list)
On May 26, 2009, at 3:17 PM, Joel Dezenzio 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 Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
B6a2e2c1454c56eb686c5a38b5f73d02?d=identicon&s=25 Joel Dezenzio (elricstorm)
on 2009-05-26 21:46
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?
08995c0e987a0b6d452a791369f96370?d=identicon&s=25 Alex (Guest)
on 2009-05-26 21:58
(Received via mailing list)
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
B6a2e2c1454c56eb686c5a38b5f73d02?d=identicon&s=25 Joel Dezenzio (elricstorm)
on 2009-05-26 22:06
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.
08995c0e987a0b6d452a791369f96370?d=identicon&s=25 Alex (Guest)
on 2009-05-26 22:17
(Received via mailing list)
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
This topic is locked and can not be replied to.