I am using the following code to insert array value from same line of input
s = Array.new(10)
q = gets
s = q.split(' ')
It is working fine. but if I don't want to take variable to store at first and split after that but directly take input in the array by using following code I fail.
s = Array.new(10)
10.times do
s.push gets.split.map(&:to_i)
end
What is the correct code to take integer inputs from same line? Need help.
You don't have to declare the array first. split method returns an array, and you can assign it to the variable. This will do
s = gets.split(' ').map &:to_i
Related
here i have the sample code , mapping array to hash and appending hash to array but expected output is missing the array value
data = {"a"=>5,"b"=>["e","f"]}
data1 = [44,55]
s = []
data1.each_with_index do |i,index|
a = data1[index]
data["b"] = i
s << data
end
p s
output:
[{"a"=>5, "b"=>55}, {"a"=>5, "b"=>55}]
Expected output:
[{"a"=>5, "b"=>44}, {"a"=>5, "b"=>55}]
Your main problem is that you are putting the same object reference into each of your array elements. Think of it as putting two keys to the same locker in two different places. If you change the stuff in the locker, both keys will open it, and they will both see the same stuff. So, when you change the value of 'b', you change it for the object in both array elements, because they're the same object.
The solution is to start your block by creating a copy of the data object, changing its 'b' value, and putting the result into the resulting array.
I've also changed your each_with_index to map, because map is best to use when you are transforming the values in the array. Also, you don't need to have the index available in your block; you're just running through the array elements without caring which element you're working with while you're working with it.
data = {"a"=>5,"b"=>["e","f"]}
data1 = [44,55]
s = []
data1.map do |num|
the_copy = data.clone
the_copy['b'] = num
s << the_copy
end
p s
I'm working on a mini project for a summer class. I'd like some feedback on the code I have written, especially part 3.
Here's the question:
Create an array called numbers containing the integers 1 - 10 and assign it to a variable.
Create an empty array called even_numbers.
Create a method that iterates over the array. Place all even numbers in the array even_numbers.
Print the array even_numbers.
Here's my code, so far:
numbers = [1,2,3,4,5,6,7,8,9,10]
print numbers[3]
even_numbers.empty?
def even_numbers
numbers.sort!
end
Rather than doing explicit iteration, the best way is likely Array#select thus:
even_numbers = numbers.select { |n| n.even? }
which will run the block given on each element in the array numbers and produce an array containing all elements for which the block returned true.
or an alternative solution following the convention of your problem:
def get_even_numbers(array)
even_num = []
array.each do |n|
even_num << n if n.even?
end
even_num
end
and of course going for the select method is always preferred.
I want to change a value of a two dimensional array.
This is the array:
class Test
def initialize
#single = [1,2,3,4,5,6,7,8,9,10]
#double = [#single, #single, #single, #single]
end
def changeValue i, j
#double[i][j] = nil
end
def showDouble
return #double
end
end
I want to change a value in the double array (the two dimensional array). If I want to change the value of 9 in the first array, then I should do something like this:
test = Test.new
test.changeValue 0, 8
puts test.showDouble
When I do this, then the value of 9 is in every array nil. I only want to change it in one array. Any help is welcome! :)
The array #double actually contains four references to the same array #single, which is why you're getting the behavior you describe.
Initialize #double = [#single.clone, #single.clone, #single.clone, #single.clone] to get independent (but initially identical) sub-arrays.
Here
#double = [#single, #single, #single, #single]
you fill array with same object, in changeValue you change it, so it is being changed 4 times for #double. If you want 4 different objects, init #double as:
#double = [#single.dup, #single.dup, #single.dup, #single.dup]
I have a ruby problem
Here's what i'm trying to do
def iterate1 #define method in given class
#var3 = #var2.split(" ") #split string to array
#var4 = #var3
#var4.each do |i| #for each array item do i
ra = []
i.each_char {|d| ra << counter1(d)} # for each char in i, apply def counter1
#sum = ra.inject(:+)
#sum2 = #sum.inject(:+) #have to do the inject twice to get values
end
#sum2
I know i have over complicated this
Basically the input is a string of letters and values like "14556 this word 398"
I am trying to sum the numbers in each value, seperated by the whitespace like (" ")
When i use the def iterate1 method the block calls the counter1 method just fine, but i can only get the value for the last word or value in the string.
In this case that's 398, which when summed would be 27.
If i include a break i get the first value, which would be 21.
I'm looking to output an array with all of the summed values
Any help would be greatly appreciated
I think you're after:
"10 d 20 c".scan(/\b\d+\b/).map(&:to_i).inject(:+) # Returns 30
scan(/\b\d+\b/) will extract all numbers that are made up of digits only in an array, map(&:to_i) will convert them to integers and I guess you already know what inject(:+) will do.
I'm not sure if I understand what you're after correctly, though, so it might help if you provide the answer you expect to this input.
EDIT:
If you want to sum the digits in each number, you can do it with:
"12 d 34 c".scan(/\b\d+\b/).map { |x| x.chars.map(&:to_i).inject(:+) }
x.chars will return an enumerator for the digits, map(&:to_i) will convert them to integers and inject(:+) will sum them.
The simplest answer is to use map instead of each because the former collects the results and returns an array. e.g:
def iterate1 #define method in given class
#var3 = #var2.split(" ") #split string to array
#var4 = #var3
#var4.map do |i| #for each array item do i
ra = []
i.each_char {|d| ra << counter1(d)} # for each char in i, apply def counter1
#sum = ra.inject(:+)
#sum2 = #sum.inject(:+) #have to do the inject twice to get values
end
end
You could write it a lot cleaner though and I think Stefan was a big help. You could solve the issue with a little modification of his code
# when you call iterate, you should pass in the value
# even if you have an instance variable available (e.g. #var2)
def iterate(thing)
thing.scan(/\b\d+\b/).map do |x|
x.chars.map{|d| counter1(d)}.inject(:+)
end
end
The above assumes that the counter1 method returns back the value as an integer
I'm fairly new to ruby. I have the following csv:
Office (1), Test
Office (Test)(2), Test
In "data.csv".
Then in my ruby script I have;
CSV.foreach("data.csv") do |line|
registeredOffice = line[0].to_s()
macOffice = registeredOffice.scan(/\(([^\)]+)\)/).last
csvText = "#{csvText}\n#{macOffice}"
end
Which gives me
["1"]
["2"]
However I want to know how to convert the above to a string so the output is
1
2
Using .join or [0] returns a nil:NilClass (NoMethodError)
You probably want something like:
macOffice = registeredOffice[/(\d+)\)$/, 1]
scan with capture groups will give you multi-d arrays
The following line:
macOffice = registeredOffice.scan(/\(([^\)]+)\)/).last
returns array since scan returns array of array. For the first line of data.csv, it is ["1"].
I guess you need scalar value for macOffice, thus, you want to use match which only returns non-repetive match using match, which returns array of matches once. For example, you can grab first match from the returned array using [1] subscript, thus:
macOffice = registeredOffice.match(/\(([^\)]+)\)/)[1]
which returns 1.
Assuming you want an array you can write like this:
out = []
CSV.foreach("data.csv") do |line|
registeredOffice = line[0].to_s()
macOffice = registeredOffice.match(/\((\d+)\)/)[1]
out.push(macOffice)
end
puts out.join(",")
to produce 1,2