Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Is there a way to find a value of in an array of class objects. Then once it is found to be included, can you find what place within the array it resides at? E.g.
class SomeClass
def initialize(value,otherData)
#value = value
#otherData = otherData
end
end
x = 0
otherData = "foo"
list = Array.new
while (x < 3)
list.push SomeClass.new(x,otherData)
x += 1
end
I want to find list[x].value where value=2 and figure out what place in the array that is located at.
If SomeClass had a value method, an obvious way would be:
ix = list.find_index { |sc| sc.value == 2 }
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
How do I swap an(example) array's second and third elements? I tried the following
def swap_elements(array)
array = ["blake", "ashley", "scott"]
array[1], array[2] = array[2], array[1]
end
but I get
: ["scott", "ashley"]
I lost the first[0] element
There are many ways to do this - this is a functional approach that doesn't mutate the original array:
def swap_elements(array)
# yields the array to the block
array.then do |first, *rest| # deconstruct the array
rest.reverse # swap the places of 2 & 3
.unshift(first) # put the first back in
end
end
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have 2 arrays like and , I want to generate a new array with 2 elements like pID and cNo. pID is getting from the first array and the cNO is from the second array. How to merge these 2 arrays like
newarr= { 0: pID:970989, cID:'dfds'
1: pID:970995, cID:'fgd',
2: pID:971006, cID:'t765'}
Assuming your arrays are equal in length you can just loop over one of them and create a new array.
const newArray = pIDArray.map((pID, index) => ({ pID, CID: cIDArray[index] }));
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
S.a=rand(100,3)
S.b=rand(100,3)
S.c=rand(100,3)
S.d=rand(100,3)
K.a=[ ]
K.b=[ ]
K.c=[ ]
K.d=[ ]
for i=1:numel(S)
if rand<0.8 % condition
K(i,:)=S(i,:) % How this assignment can be made
end
end
How to extract the rows of all the fields of a structure and store it to another structure.
I'm not entirely sure from you explanation if this is what you want. The following code will copy the fields from in S into K for just the rows where the items are < 0.8
S.a=rand(100,3);
S.b=rand(100,3);
S.c=rand(100,3);
S.d=rand(100,3);
K = [];
for field = ['a', 'b', 'c', 'd']
I = rand(length(S.(field)), 1) < 0.8;
K.(field) = S.(field)(I,:);
end
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Given an array of letters:
#recipients_list = ["Mum and Dad", "my best friend", "Brother"]
# > Here are the letters available to ship, select one by number
# > 1. Mum and Dad
# > 2. My best friend
# > 3. Brother
As a user I then enter 3 (notice that Brother in this case is the recipient name). My code:
x = #recipient_list.each_with_index do |value, i|
index_start = i + 1
puts "#{index_start}.#{value}"
end
input = gets.chomp.to_i
puts x.at(input)
How to get brother value from array when user press 3? Anyone can help?
You have to use puts x.at(input - 1) because of Ruby arrays' indexing starts with 0
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have 2 arrays of strings as under. I want to compare them and execute some code if these arrays are not equal-
current_instances = ["170601_7711", "170601_8811"]
app_instances = ["170602_7711", "170602_8811"]
How can I compare them in ruby?
x = ["alpha1", "beta1"]
y = ["alpha2", "beta2"]
Check if all elements are the same
x == y #=> false
Compare each element
Compare each string at some index i, assuming both arrays are the same size. Then apply some code to the matches.
x.zip(y) #=> [["alpha1", "alpha2"], ["beta1", "beta2"]]
x.zip(y).map {|a,b| a == b ? 'do this' : 'else do this' }
#=> ["else do this", "else do this"]
Perhaps you can do an array difference with the - operator and execute your code if the difference is zero
arr_diff = current_instances - app_instances
This is the simplest solution I could think of :)
Check my solution and let me know how it goes