Trying to remove invalid characters

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}/)}"

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:

�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,

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 Blue 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,