How to evaluate >= when data is in a string?

Hi:

I have a comma separated values’ string where all data is text (letters “A” or “I”) and I’m doing some comparisons if ==A or ==I. This works fine. Now I want to also ask if the data is bigger or between several numbers that I’m adding to the same text file on top. So on top of all my rows of text I really have a row of numbers (really, they are strings that could be read as numbers or converted). How can I do it? For what I see, the code is not doing what I expect. The evaluated string is not converted to a number or I’m doing something wrong.

I have this piece of code

if(t[i,1] == ‘A’) then
inst.material = [253,141,60]
elsif (t[i,1] == ‘I’)
inst.material = “blue”
inst.material.alpha = 0.1

And now this is where I ask for the string (converted to float) is equal or smaller than 2
elsif(t[i,1].to_f <= 2)
inst.material = [254,237,222]

I see that all the evaluations are considered the same because the material is always the same.

The full comparison is his

if(t[i,1] == ‘A’) then
inst.material = [253,141,60]
inst.material.alpha = 0.5
elsif (t[i,1] == ‘I’)
inst.material = “blue”
inst.material.alpha = 0.1
elsif(t[i,1].to_f <= 2)
inst.material = [254,237,222]
elsif (t[i,1].to_f > 2) and (t[i,1].to_f <= 4)
inst.material = [253,190,133]
elsif (t[i,1].to_f > 4) and (t[i,1].to_f <= 6)
inst.material = [253,141,60]
elsif (t[i,1].to_f > 6) and (t[i,1].to_f <= 8)
inst.material = [230,85,13]
else
inst.material = [166,54,3]
end

Some tips for you.

Say that data_string is one of your comma separated strings that you read from a file.
Convert it to an array of data:
data = data_string.split(',')


Be careful with String#to_f, as it never raises an exception. If an error occurs, or it cannot convert to Float, it will simply return 0.0 !

I need to point out that you are comparing floats with integers. There is a similar method String#to_i.


Some classes mix in the Comparable library module. It has the between? method.
puts("Bigger than 2 and smaller than 4!") if num.between?(2,4)


You can use case statements and literal Range class objects to make your code more readable:

obj = t[i,1]
if obj == 'A'
  inst.material = [253,141,60]
  inst.material.alpha = 0.5
elsif obj == 'I'
  inst.material = "blue"
  inst.material.alpha = 0.1
else
  num = obj.to_f
  case num
  when 0.0..2.0
    inst.material = [254,237,222]
  when 2.0..4.0
    inst.material = [253,190,133]
  when 4.0..6.0
    inst.material = [253,141,60]
  when 6.0..8.0
    inst.material = [230,85,13]
  else
    inst.material = [166,54,3]
  end # case
end # if

Unnecessary use of ( ) and then are frowned upon, and are listed as “donts” in most Ruby style guides. They just clutter up the code.


And here in the forums, please wrap code in backticks, like:

And now this is where I ask if the string (converted to float) is equal or smaller than 2:
[blank line]
```ruby
elsif(t[i,1].to_f <= 2)
  inst.material = [254,237,222]
```

“[blank line]” means leave an empty line (CR), not to actually type “[blank line]” there.

Which renders like:

And now this is where I ask if the string (converted to float) is equal or smaller than 2:

elsif(t[i,1].to_f <= 2)
inst.material = [254,237,222]

Thanks Dan!

In this case, just using puts to see the array I’ve found that as always with me, error == human error
I had a whole file full of rows of characters without commas and the one I was adding was CSV… Split was lost :smile:

I’ve fixed it and now works perfect, looks better and I’ve learnt something today.

Thanks a lot again.