# Trying to remove invalid characters

**URL:** <https://www.ruby-forum.com/t/trying-to-remove-invalid-characters/177915>\
**Category:** Rails\
**Created:** [October 26, 2009, 4:54pm UTC](https://www.ruby-forum.com/t/trying-to-remove-invalid-characters/177915 "2009-10-26T16:54:35Z")\
**Posts on this page:** 9\
**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:** [October 26, 2009, 4:54pm UTC](https://www.ruby-forum.com/t/trying-to-remove-invalid-characters/177915/1 "2009-10-26T16:54:35Z")

</div>

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

---

<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:** [October 26, 2009, 4:59pm UTC](https://www.ruby-forum.com/t/trying-to-remove-invalid-characters/177915/2 "2009-10-26T16:59:06Z")

</div>

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…

---

<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:** [October 26, 2009, 7:32pm UTC](https://www.ruby-forum.com/t/trying-to-remove-invalid-characters/177915/3 "2009-10-26T19:32:44Z")

</div>

On Oct 26, 11:59 am, Alpha B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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

---

<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:** [October 26, 2009, 7:50pm UTC](https://www.ruby-forum.com/t/trying-to-remove-invalid-characters/177915/4 "2009-10-26T19:50:16Z")

</div>

Wes wrote:

> On Oct 26, 11:59ï¿½am, Alpha B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> 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](http://www.marnen.org)  
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:** [October 26, 2009, 5:07pm UTC](https://www.ruby-forum.com/t/trying-to-remove-invalid-characters/177915/5 "2009-10-26T17:07:49Z")

</div>

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

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
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:** [October 26, 2009, 9:19pm UTC](https://www.ruby-forum.com/t/trying-to-remove-invalid-characters/177915/6 "2009-10-26T21:19:45Z")

</div>

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.

---

<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:** [October 26, 2009, 11:14pm UTC](https://www.ruby-forum.com/t/trying-to-remove-invalid-characters/177915/7 "2009-10-26T23:14:01Z")

</div>

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.

---

<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:** [October 26, 2009, 9:40pm UTC](https://www.ruby-forum.com/t/trying-to-remove-invalid-characters/177915/8 "2009-10-26T21:40:31Z")

</div>

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](http://www.marnen.org)  
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:** [October 27, 2009, 5:20am UTC](https://www.ruby-forum.com/t/trying-to-remove-invalid-characters/177915/9 "2009-10-27T05:20:15Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid
