I want to know how to read a number from a file to a variable. Anyone able to help please?
If the entire file's contents is the number, I'd use File::read to get the contents of the file and String#to_i to convert the resulting string to an integer.
So:
number = File.read('filename.txt').to_i
If your file has a string or variable length of characters that has some numbers in it then you can get all the numbers using regex and assign it to your variable e.g.
file_contents = File.read('filename') # => "a string of character with 23 number and 123 in it"
numbers = file_contents.scan(/\d+/) # => ['23', '123']
To convert the above array of string numbers to integer
numbers.collect! &:to_i # => [23, 123]
Then you can assign these number to any variable you want
number1 = numbers.first
number2 = numbers.last
Related
I want to split the last string from given path, that string contains some numbers like 1.625.235, but this numbers vary every time. Irrespective of the number that last string should be split.
Ex:
string = "C:/chef/cache/Dynatrace-OneAgent-Windows-1.625.235.msi"
output: Dynatrace-OneAgent-Windows-1.625.235.msi
string = "C:/chef/cache/Dynatrace-OneAgent-Windows-1.181.539.msi"
output: Dynatrace-OneAgent-Windows-1.181.539.msi
This is what we tried
("C:/chef/cache/Dynatrace-OneAgent-Windows-/\d.\d+.\d+/.msi").split('/')[3]
("C:/chef/cache/Dynatrace-OneAgent-Windows-'/\d.\d+.\d+/'.msi").split('/')[3]
("C:/chef/cache/Dynatrace-OneAgent-Windows-'\d.\d+.\d+'.msi").split('/')[3]
("C:/chef/cache/Dynatrace-OneAgent-Windows-'(\d.\d+.\d+').msi").split('/')[3]
("C:/chef/cache/Dynatrace-OneAgent-Windows-('/\d.\d+.\d+/').msi").split('/')[3]
("C:/chef/cache/Dynatrace-OneAgent-Windows-('\d.\d+.\d+').msi").split('/')[3]
If the output you want is always the filename at the end of a path, you could also use File.basename.
string = "C:/chef/cache/Dynatrace-OneAgent-Windows-1.625.235.msi"
output = File.basename(string) # => "Dynatrace-OneAgent-Windows-1.625.235.msi"
string="C:/chef/cache/Dynatrace-OneAgent-Windows-1.625.235.msi"
p string.split("/").last
output
"Dynatrace-OneAgent-Windows-1.625.235.msi"
I'm trying to take the user's input (numbers separated by commas, e.g., "5,8,11"), and return the equivalent number of "-"s. For example, if the user inputs "4,2,4,5", then the output should be the following:
----
--
----
-----
with each on a new line. I need to take an input string, split it at the commas, which will turn it into an array, and then iterate through the array and print the amount of commas per element.
I tried this,
puts "Enter some numbers"
input = gets.chomp
input.split(',')
input.each do |times|
puts "-" * times
end
which returns a noMethodError. I'm not sure where I am wrong.
Any help would be greatly appreciated.
You need integers for that. Try
input = gets.chomp.split(',').map(&:to_i)
Couple of things...
input.split(',')
This DOES split input, but it doesn't change the contents of the input variable.
What would work...
input = input.split(',')
Secondly, the result will be an array of strings, not integers, so better would be...
input = input.split(',').map(&:to_i)
This will map the string array into an integer array
I'm trying to decode this file that is in IBM437 into readable UTF I'm at the point where I think I've almost got it but I'm getting an ArgumentError where the string contains nul bytes, I'm aware of how to gsub out nul bytes using:
.gsub("\u0000", '') however I can't figure out where to gsub the bytes out.
Here's the source:
def gather_info
file = './lib/SETI_message.txt'
File.read(file).each_line do |gather|
packed = [gather].pack('b*')
ec = Encoding::Converter.new(packed, 'utf-8')
encoding_forced = packed.encode(ec)
File.open('packed.txt', 'a+'){ |s| s.puts(encoding_forced.gsub("\u0000", '')) }
end
end
gather_info
And here's the file
Can anyone tell me what I'm doing wrong here?
The following works for me :
file = File.read('SETI.txt')
packed = file.scan(/......../).map{|s| s.to_i(2)}.pack('U*')
File.write('packed.txt', packed)
Let's break file.scan(/......../).map{|s| s.to_i(2)}.pack('U*') down :
file.scan(/......../)
Here we break the huge string of 0s and 1s (the file) into an array of strings containing 8 characters each. It looks like that : ['00001111', '11110000', ...].
arr.map{|s| s.to_i(2)}
From step 1 we got an array of strings representing the different characters in binary notation. We can convert one of those strings (called s) by applying s.to_i(2) because the parameter '2' says to the method to_i to use base 2. So '00000011'.to_i(2) returns 3.
We apply this to all the characters by using map.
So we now have an array that looks like [98, 82, 49, 39, ...].
arr.pack('U*')
From step 2 we have an array of integers representing each a character. We can now use the pack method to transform our array of integers into a string. The parameter we use for pack is U to tell him that the integers are in fact UTF-8 characters.
I want to find a specific character in a given string of number for example if my input is:
1 4 5 7 9 12
Then for 4 the answer should be 1. My code is as follows:
secarr = second.split(" ")
answer = secarr.index(number) #here number is a variable which gets the character
puts answer
The above method works if I write "4" instead of number or any other specific character but does not work if I write a variable. Is there a method in ruby to do the same?
This is probably your variable number is an Integer, and secarr is an Array of Strings. Try to cast the number to string:
answer = secarr.index(number.to_s)
I have strings like this:
https://www.facebook.com/username_with_number_14/posts/101505775425654414
https://www.facebook.com/username/posts/101505775425654466
I need to extract the number on the end of the string in Ruby. In the first string, it is the second and last number, whereas in the second string it is the first, only and last number.
At the moment I am extracting the number like this:
int1 = Regexp.new('.*?(\\d+)',Regexp::IGNORECASE).match()[1]
But when this is applied to the first string, it extracts the number part of the username, not the desired number.
How can I do it so that it will work on both strings?
text = <<ENDTEXT
https://www.facebook.com/username_with_number_14/posts/101505775425654414
https://www.facebook.com/username/posts/101505775425654466
ENDTEXT
p text.lines.map{|line| line.scan(/\d+/).last}
#=> ["101505775425654414", "101505775425654466"]
for me works regexp like this:
^.*?(\d+)$
look here: http://rubular.com/r/CJzsgjedqJ
Try this
int1 = Regexp.new('.*\\/(\\d+)$',Regexp::IGNORECASE).match()[1]
The $ matches the end of the string. So I put all numbers from the last / to the end of the string into the capturing group 1.