[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Replace empty or strange characters within array of arrays

Joel Dezenzio

5/26/2009 7:18:00 PM

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

Attachments:
http://www.ruby-...attachment/3735/...

--
Posted via http://www.ruby-....

5 Answers

Rob Biedenharn

5/26/2009 7:41:00 PM

0


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 ( =C2 ).


Actually, on the original site, the cell is not empty, but contains a =20=

  so you might want to test for that.

It seems like you want to assign the same value to any such row in the =20=

"provisional" table (i.e., they all tie for the next available place).

-Rob

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com


Joel Dezenzio

5/26/2009 7:46:00 PM

0

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?

--
Posted via http://www.ruby-....

imphasing

5/26/2009 7:58:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

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


On Tue, May 26, 2009 at 3:46 PM, Joel Dezenzio <jdezenzio@gmail.com> wrote:

> 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?
>
> --
> Posted via http://www.ruby-....
>
>

Joel Dezenzio

5/26/2009 8:07:00 PM

0

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.

--
Posted via http://www.ruby-....

imphasing

5/26/2009 8:17:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

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

On Tue, May 26, 2009 at 4:07 PM, Joel Dezenzio <jdezenzio@gmail.com> wrote:

> 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.
>
> --
> Posted via http://www.ruby-....
>
>