Trying to remove invalid characters

When parsing win/loss scores from ESPN, I notice that the output looks
perfectly normal, even when I carefully inspect the output.

It looks like this:

wloss = /^\w\s\d{1,2}-\d{1,2}/
puts @rows[i][3]
@rows[i][4] = @rows[i][3].scan(wloss)
puts @rows[i][4]

L 31-27 (this is @rows[i][3])
L 31-27 (this is @rows[i][4])
W 32-14 etc…
W 32-14
L 27-14
L 27-14
W 38-22
W 38-22
W 16-13 OT
W 16-13
W 63-14
W 63-14
W 38-35 OT
W 38-35
W 13-10
W 13-10

I scan and keep only the valid data with @rows[i][4]:

If the rows are empty or not in this format, I just return an empty
array.

When I upload this same visible data to my database, it ends up
appearing as:


  • L 31-27

  • W 32-14

  • L 27-14

  • W 38-22
    etc.
    etc.

If the row is empty in the database it appears as:


  • []

So, in my view I end up having to use:

<%=h schedule.winloss.scan(/\w\s\d{1,2}-\d{1,2}/) %>

If I try to sub /—/ out of the line, it does nothing. I have no idea
why it’s showing up there and why the puts output doesn’t even show it.
Are these some type of invisible characters that are meant to denote
spaces of some sort?

Any help would be appreciated. I just want to clean my database so that
it does not show the dashes — -.

This is pretty funny.

Just by typing out the issue, I just found my problem.

When uploading to the database I was doing:

win_loss = myvars[i].scan(/\w\s\d{1,2}-\d{1,2}/)

Instead of …

win_loss = “#{myvars[i].scan(/\w\s\d{1,2}-\d{1,2}/)}”

I realized this when I saw the empty [] array listed in the database.

Problem fixed. My apologies…

On Oct 26, 11:59 am, Alpha B. [email protected]
wrote:

win_loss = “#{myvars[i].scan(/\w\s\d{1,2}-\d{1,2}/)}”
You can also do:

myvars[i].scan(/\w\s\d{1,2}-\d{1,2}/).to_s

It will do the right thing when no match is found, etc.

– W

Wes wrote:

On Oct 26, 11:59�am, Alpha B. [email protected]
wrote:

�win_loss = “#{myvars[i].scan(/\w\s\d{1,2}-\d{1,2}/)}”
You can also do:

myvars[i].scan(/\w\s\d{1,2}-\d{1,2}/).to_s

Excellent point. If you’re #{interpolating} the whole string, you might
just as well use the expression without quotes.

It will do the right thing when no match is found, etc.

– W

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Alpha B. wrote:

When parsing win/loss scores from ESPN, I notice that the output looks
perfectly normal, even when I carefully inspect the output.

It looks like this:

wloss = /^\w\s\d{1,2}-\d{1,2}/
puts @rows[i][3]
[…]

Unrelated to your issue, but…what’s with the [i] ? If you want to
write PHP, do it in PHP. If you want to write Ruby, use .each. :slight_smile:

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Thanks fellas. I took the latter tip to heart and removed the quotes
and used .to_s

As for the [i], I’m iterating over multiple queries that have the exact
same row count. Therefore, I don’t need to use each. I do use each and
each_with_index a lot.

(1…120).each do |i|

end

… this suffices and allows me to count the exact number of iterations I
need. In cases where I don’t know the row count, or the row count isn’t
the same count across multiple tables, I don’t use the above.

Thanks.

Hi Marnen,

I see what you are saying and yes, that makes a lot of sense. I do use
each and often. I rarely use the (1…120) but could I perhaps do:

datasize = Team.all.size
(1…datasize).each

?

What I’m trying to accomplish and I only have to do this in a few
instances is that there will be times that I need to iterate over x
number of teams but I don’t need to iterate over a returned query.

In otherwords, I’m creating data not pulling data.

I can understand if I do something along the line of:

team = Team.all

team.each do |row|

end

But, I’m not working with the teams query, or any other query. I’m
working with actual data that I need to iterate over x number of teams
(by count) which happens to be 120 teams. However, you bring up a valid
point in that if another team enters FBS, I’d have to change the hard
count of the data iteration from 120 to 121. This is why I believe
doing the .size would suffice.

Unless, you think there is another way?

Thanks.

Alpha B. wrote:

Thanks fellas. I took the latter tip to heart and removed the quotes
and used .to_s

As for the [i], I’m iterating over multiple queries that have the exact
same row count. Therefore, I don’t need to use each. I do use each and
each_with_index a lot.

(1…120).each do |i|

end

… this suffices and allows me to count the exact number of iterations I
need.

NO! NO! NO!

You’ve hard-coded the value 120. There’s absolutely NO reason to do
that, and it just makes your code more brittle. Worse, you’re using a
“magic number” to do it, not a symbolic constant, so it will be harder
to change each instance when the next team joins the NCAA. If you just
did @rows.each, you’d never need to change it.

Why introduce pointless couplings and dependencies into your code? The
array already knows how many records it contains, so let it keep track
itself.

In cases where I don’t know the row count, or the row count isn’t
the same count across multiple tables, I don’t use the above.

You should only ever use that syntax if you’re actually dealing with the
numbers 1 to 120 as data (say, if you’re calculating the first 120
factorial numbers). If you want to iterate over a recordset, always
use each.

Thanks.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Alpha B. wrote:

Hi Marnen,

I see what you are saying and yes, that makes a lot of sense. I do use
each and often. I rarely use the (1…120) but could I perhaps do:

datasize = Team.all.size
(1…datasize).each

?

If you were to do it that way, then you’d want to use Team.count. But
you probably don’t need the extra query…

What I’m trying to accomplish and I only have to do this in a few
instances is that there will be times that I need to iterate over x
number of teams but I don’t need to iterate over a returned query.

Then why do you need to iterate over the 120 teams? You must have 120
of something if you are in a position where you need to do this.

In otherwords, I’m creating data not pulling data.

I can understand if I do something along the line of:

team = Team.all

team.each do |row|

end

But, I’m not working with the teams query, or any other query. I’m
working with actual data that I need to iterate over x number of teams
(by count) which happens to be 120 teams.

So…what’s the structure of that data?

However, you bring up a valid
point in that if another team enters FBS, I’d have to change the hard
count of the data iteration from 120 to 121.

Yeah, magic numbers are generally bad.

This is why I believe
doing the .size would suffice.

Except that it should be .count, so that it can use the DB’s count(*)
function.

Unless, you think there is another way?

I think it’s likely. The reason I asked about the structure of your
data is that I strongly suspect that some part of it is an array of 120
elements. If not, then how are you using your loop index?

Thanks.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs