Hi I'm trying to parse an Xml file to add it to my database.
But one of the field is a string, i mean : "101m" and i wanna convert it to an int to have just 101.
Ty for your help
In ruby, to_i is used to convert objects to integers. Here it is on the console:
>> "101m".to_i
=> 101
>> "101m23".to_i
=> 101
As you can see, .to_i starts from the left and gives up as soon as it finds something that can't be sensibly converted to an integer.
Related
I have an app UI that expects a HEX value e.g. foo = 0x113
I'm doing this in Lua to try to write to foo:
menu.set("Presets", "foo", "0x318")
menu.set("Presets", "888x", "-258")
menu.set("Presets", "89ab", "-60"
The values for 888x and 89ab in the app are set. The HEX value field remains empty. Could someone help please? Thanks.
There is no such thing as an hex value. There are numbers expressed in hex.
So your API expects a number. No wonder "0x318" does not work. The other two work because the strings are convertible to numbers.
Bottom line: use menu.set("Presets", "foo", 0x318).
I have a problem with mysql and certain characters. If a user enters "hello ●", I obtain this error:
Mysql2::Error: Incorrect string value: '\\xE2\\x97\\x8F he...' for column 'subject'
I would like to exclude all characters whose bytesize is greater than two, i.e., keep French characters like é, à, ç, and remove emojis or characters like ●.
Given string = "hèllö>●!", I would like to obtain "hèllö>!". In order to do so, I wrote this:
def bytesize(var)
var.each_char do |char|
puts char.bytesize
end
end
bytesize(string)
1
2
1
1
2
1
3
1
# => "hèllö>●!"
which is not what I expected. What is the best way to remove from all characters whose the bytesize is greater than two from a string?
I don't do that in the model because I can manage this with a gem, but my problem appears when a job wants to put the string in the logs of Amazon SES.
Elaborating on OP's efforts, not using regular expressions:
string = "hèllö>●!"
cleaned = string.each_char.with_object("") do |char, str|
str << char unless char.bytesize > 2
end
p cleaned
I suspect that you are getting that error message because you have the wrong column text encoding. If you are using Unicode in your system, and this day and age you should be, your column type should be utf8mb4. See this on how to change your column types.
Taking your comment into account the following will remove any characters outside the BMP
sentence.gsub(/[\u{10000}-\u{10FFFF}]/,'')
My Ruby environment is: Ruby 2.3.1 and Rails 5.0.0.1.
I'm trying to convert a negative string number in an integer, for instance:
When I try to turn this "-2000" in the irb terminal, I got the result expected -2000
But I'm trying to convert this when I import this data from a CSV file.
I'm using the following information:
CSV file
345,-2000
345,120000
Code file
CSV.foreach("file.csv") do |row|
p [row[0], row[1]]
p row[1].to_i
p row[1].force_encoding('UTF-8').to_i
p Integer(row[1])
p Integer(row[1].force_encoding('UTF-8'))
end
I got that:
["345", "-2000"]
0
0
'Integer': invalid value for Integer(): "\xC2\xAD2000" (ArgumentError)
'Integer': invalid value for Integer(): "\xC2\xAD2000" (ArgumentError)
Using the Integer(), I discovered that the - sign is represented by "\xC2\xAD".
In summary, the to_i method is converting "\xC2\xAD2000" to 0 and the Integer() is trigging an error.
Could someone help with that?
Thanks for your attention.
It looks like you actually have two characters here..
\xC2: SublimeText keeps inserting \xc2 characters
\xAD: (soft-hyphen) http://www.fileformat.info/info/unicode/char/00ad/index.htm
Personally, I would replace this combination of characters with an actual hyphen and then convert to integer:
CSV.foreach("file.csv") do |row|
p row[1].sub("\xC2\xAD", '-').to_i
end
That, or clean up the source file. Unsure of how you are generating it, but worth looking into.
I have a text file which read:
config<001>25
23<220>12
.....
how can i parse so that i need only the values config,001(to be converted into integer after extracting using strtok or any ohter methods please suggest), and 25(to be converted into integer) seperately. i tries strtok its not working as the way i need. Please help me.
Use LINQ 2 SQL to import the file on the delimiters and then use something like AutoMapper to do the mapping of fields to say specific objects with specific types.
I did this exact thing in another project and it works great.
Based on the mention of strtok I'm guessing that you're using C or C++. If you're using C++, I'd probably handle this by creating a ctype facet that treats < and > as white space, which will make the parsing trivial (infile >> string >> number1 >> number2;).
If you're using C, you can use the scan-set conversion with scanf, something like: sscanf(line, "%[^<] %d> %d", string, &number1, &number2);
I am wondering what is a convenient function in Rails to convert a string with a negative sign into a number. e.g. -1005.32
When I use the .to_f method, the number becomes 1005 with the negative sign and decimal part being ignored.
.to_f is the right way.
Example:
irb(main):001:0> "-10".to_f
=> -10.0
irb(main):002:0> "-10.33".to_f
=> -10.33
Maybe your string does not include a regular "-" (dash)? Or is there a space between the dash and the first numeral?
Added:
If you know that your input string is a string version of a floating number, eg, "10.2", then .to_f is the best/simplest way to do the conversion.
If you're not sure of the string's content, then using .to_f will give 0 in the case where you don't have any numbers in the string. It will give various other values depending on your input string too. Eg
irb(main):001:0> "".to_f
=> 0.0
irb(main):002:0> "hi!".to_f
=> 0.0
irb(main):003:0> "4 you!".to_f
=> 4.0
The above .to_f behavior may be just what you want, it depends on your problem case.
Depending on what you want to do in various error cases, you can use Kernel::Float as Mark Rushakoff suggests, since it raises an error when it is not perfectly happy with converting the input string.
You should be using Kernel::Float to convert the number; on invalid input, this will raise an error instead of just "trying" to convert it.
>> "10.5".to_f
=> 10.5
>> "asdf".to_f # do you *really* want a zero for this?
=> 0.0
>> Float("asdf")
ArgumentError: invalid value for Float(): "asdf"
from (irb):11:in `Float'
from (irb):11
>> Float("10.5")
=> 10.5