Get variable whose value is the maximum in an array - arrays

If I have an array of variables in Ruby:
a = 4
b = 7
c = 1
array = [a, b, c]
How can I get access to the name of the variable that has the highest value? (In this example b) I want to retrieve a reference to the element with the highest value, to be able to later manipulate it:
b += 10
I tried array.max but that just returns the max value 7

When you build an array by writing array = [a, b, c], the spots in the array do not retain any kind of association with the variables named a, b, and c, so you there is no way to do exactly what you are talking about. Those variables can change without affecting the array.
You could change the way you are storing your data in order to make this possible, but without knowing what data you are storing it is hard to recommend a good way to do it. You could consider storing your data inside a hash instead of in loose variables and an array:
hash = { a: 4, b: 7, c: 1}

Ruby does not support passing objects by reference, which is essentially what you are trying to do. When you call array.max, you are passed a copy of the Fixnum 7, and assignment and modification will be applied to this copy.
That is, you can not store a reference to this Array element, and modify it later. You can modify the maximum value on the spot, however, using:
array[array.index(array.max)] = array.max + 10
#=> 17
array
#=> [4, 17, 1]
Note that when there are duplicate values in the Array, array.index(array.max) will return the index of the first one.
Storing the index for later use is not a solid solution, since, while Arrays in Ruby are ordered, the Array or its elements can be modified between the point you retrieve the index, and the point where you decide to change the corresponding value.
Another answer suggests there's a hack to mimic pass-by-reference behaviour, but in doing this, you're working against the intention of the language, and it could lead to a lot of pain down the line.

If you are trying to manipulate the maximum of three values, then, you are better off doing something like this:
array.max + 10 # or any other manipulation for that matter.
If you are asking what you are asking for academic purposes, then, you could use Ruby's Binding to inspect values of local variables.
a = 4
b = 7
c = 1
array = [a, b, c]
# Select variable whose value matches with maximum of three elements
# Variable's name as symbol is obtained by this process
var = binding.send(:local_variables).select do |i|
array.max.eql? binding.local_variable_get(i)
end.first
#=> :b
# Update value of variable using symbol representing its name
binding.local_variable_set(var, binding.local_variable_get(var) + 10)
puts b
#=> 17
You could also use Binding#eval to manipulate the variable, for e.g.:
binding.eval("#{var.to_s} += 10")
#=> 17
Note that we need to use var.to_s as var contains a symbol, and we need to convert it to string to get variable name string that can be used in eval.

You can use Array#index for this..
1.9.3-p484 :008 > max_elem = array.index(array.max)
=> 1
1.9.3-p484 :009 > array[max_elem] = array[max_elem] + 10
=> 11
1.9.3-p484 :010 > array
=> [4, 17, 1]
1.9.3-p484 :011 >

Related

assigning values of array into variables in ruby

I am trying to assign the each value of an array into different variables. But I am getting error as "Expecting end."
strs = ["flower","flow","flight"]
strs.each_with_index do |x, i|
"b#{i}" = x
end
what is going wrong here ?
What you're doing is like saying
"howdy" = 1
You can't say that. That expression attempts to assign into a string literal. You can't do that. A string literal is not an lvalue (a thing that can go on the left side of an equal sign).
If you are trying to say "make a variable called howdy and assign this value to it", you can't do that because local variables cannot be created on the fly. See How to dynamically create a local variable?.
However, the real core of your issue is that you should not even want to do what you're doing. You already have an array, a wonderful thing that allows you to reference each entry by number. "flower" in your code is already strs[0], and so on. The whole point of the array is that it lets you do that. There is thus no need for individual variables with number names; the array is the variable with number names.
I'm not sure as to WHY you are attempting to create local variables when you already sort of have them. Consider the following where we simply replace your suggested b0 with b[0]:
strs = ["flower","flow","flight"]
b = strs
b[0] #=> "flower"
You could optionally convert your array to a hash using an approach similar to what you've already tried and replace b0 with b[0]:
b = {}
strs.each_with_index do |x, i|
b[i] = x
end
b[0] #=> "flower"
Or even something like this:
my_variables = {}
strs.each_with_index do |x, i|
my_variables[:"b#{i}"] = x
end
my_variables[:b0] #=> "flower"

In Ruby, during iteration of an array, how do I send multiple array elements as values for one specific hash key?

I know how to iterate through an array and I know how to create hash keys and values. I do not know how to create an array for the value and pass it multiple elements. My desired value for hash below is:
{'abc' => [1, 2, 3] , 'def' => [4,5,6,7]}
How would I achieve this hash, while iterating through array a and b below using each?
a = [1,2,3,4,5,6,7]
c = [1,2,3]
b = ['abc', 'def']
hash = {}
From your guidelines given in the comment:
While iterating through array a, if the element of iteration is included in array c, it is passed to the array value within key 'abc'. Otherwise, it is passed to other array value in key 'def'
You can do this:
hash = {}
hash['abc'] = a.select { |x| c.include?(x) }
hash['def'] = a.reject{ |x| c.include?(x) }
See Enumerable#select and Enumerable#reject. Also can take a look at Enumerable#partition which would be another good choice here, where you want to split an array into two arrays based on some condition:
in_a, not_in_a = a.partition { |x| c.include?(x) }
hash = { 'abc' => in_a, 'def' => not_in_a }
You can also do it with regular each if these fancy enumerable methods are bit too much for you:
hash = { 'abc' => [], 'def' => [] }
a.each do |x|
if c.include?(x)
hash['abc'].push(x)
else
hash['def'].push(x)
end
end
Unfortunately this question turned out not to be as interesting as I was hoping. I was hoping that the problem was this:
Knowing the hash key and a value, how can I make sure the key's value is an array and that the given value is appended to that?
For instance, start with h being {}. I have a key name :k and a value 1. I want h[:k], if it doesn't already exist, to be [1]. But if it does already exist, then it's an array and I want to append 1 to that array; for instance, if h[:k] is already [3,2], now it should be [3,2,1].
I can think of various ways to ensure that, but one possibility is this:
(hash[key] ||= []) << value
To see that this works, let's make it a method:
def add_to_hash_array_value(hash:, key:, value:)
(hash[key] ||= []) << value
end
Now I'll just call that a bunch of times:
h = {}
add_to_hash_array_value(hash:h, key:"abc", value:1)
add_to_hash_array_value(hash:h, key:"abc", value:2)
add_to_hash_array_value(hash:h, key:"def", value:4)
add_to_hash_array_value(hash:h, key:"def", value:5)
add_to_hash_array_value(hash:h, key:"abc", value:3)
puts h #=> {"abc"=>[1, 2, 3], "def"=>[4, 5]}
We got the right answer.
This is nice because suppose I have some way of knowing, given a value, what key it should be appended to. I can just repeatedly apply that decision-making process.
However, the trouble with trying to apply that to the original question is that the original question seems not to know exactly how to decide, given a value, what key it should be appended to.

Why is one of these functions changing the original array and not the other?

I'm not sure what I am missing by looking at these three functions.
The desired result for these functions would be that the original array changes value so
change_this = [6, 7, 8]
array_times_two!(change_this)
change_this == [12, 14, 16] => true
The following function does this
def array_times_two!(array)
array.map! {|x| x * 2}
end
And so does this one...
def array_times_two!(array)
array.each_with_index do |element, index|
array[index] = array[index] * 2
end
array
end
How come the following function does not change the value like the others?
def array_times_two!(array)
array = array.map {|x| x * 2}
array
end
How is the second function changing the array but the third isn't?
In the last example you have array coming in as a local variable. Reassigning the local variable has zero effect on the original variable. This is because Ruby is, in effect, pass by value, except the confusing part is those values are often object references, or a fancy term for pointers.
The consequences of this aren't that complicated though: Calling a method on an object can modify the contents of that object. Reassigning a variable cannot.
The first version of your method is probably the best, but you could make it more generic, like:
def multiply_by!(array, n = 2)
array.map! { |v| v * n }
end
You can also "fix" the last version by using a method call to apply the changes:
def array_times_two!(array)
array.replace(array.map {|x| x * 2})
end
This calls Array#replace to stomp the contents of the original object and force the changes to stick. It's not a very elegant solution when things like map! exist.

An array of arrays of different sizes

I'm learning R and I'd like to make an "array of arrays" (not sure if the expression is correct) inserting for example these values
N_seq = c(10,50,100,500,1000)
inside this function (not correct):
x = rnorm(N_seq,3.2,1)
The desired result should be like an object made by five arrays (as length(N_seq) = 5) where each one is equal to the result of x inserting each value of N_seq (so that x[1] has the values of rnorm(N_seq[i], 3.2, 1) with length 10, and x[2] has the values rnorm(N_seq[2], 3.2, 1) with length 50, etc.
For ragged array, use "list". This is a special type of "vector" in R. You can not only hold vectors of difference length in each list element, but also different type of objects for each list element.
The lapply function for "list apply" is frequently used to process a list and / or return a list. For your task, you can do:
lapply(N_seq, FUN = rnorm, mean = 3.2, sd = 1)
lapply applies function FUN to each vector elements of N_seq, where mean = 3.2 and sd = 1 are additional parameters passed to FUN, which is rnorm here.

Modify hashes in an array based on another array

I have two arrays like this:
a = [{'one'=>1, 'two'=>2},{'uno'=>1, 'dos'=>2}]
b = ['english', 'spanish']
I need to add a key-value pair to each hash in a to get this:
a = [{'one'=>1, 'two'=>2, 'language'=>'english'},{'uno'=>1, 'dos'=>2, 'language'=>'spanish'}]
I attempted this:
(0..a.length).each {|c| a[c]['language']=b[c]}
and it does not work. With this:
a[1]['language']=b[1]
(0..a.length).each {|c| puts c}
an error is shown:
NoMethodError (undefined method '[]=' for nil:NilClass)
How can I fix this?
a.zip(b){|h, v| h["language"] = v}
a # => [
# {"one"=>1, "two"=>2, "language"=>"english"},
# {"uno"=>1, "dos"=>2, "language"=>"spanish"}
# ]
When the each iterator over your Range reaches the last element (i.e. a.length), you will attempt to access a nonexisting element of a.
In your example, a.length is 2, so on the last iteration of your each, you will attempt to access a[2], which doesn't exist. (a only contains 2 elements wich indices 0 and 1.) a[2] evaluates to nil, so you will now attempt to call nil['language']=b[2], which is syntactic sugar for nil.[]=('language', b[2]), and since nil doesn't have a []= method, you get a NoMethodError.
The immediate fix is to not iterate off the end of a, by using an exclusive Range:
(0...a.length).each {|c| a[c]['language'] = b[c] }
By the way, the code you posted:
(0..a.length).each {|c| puts c }
should clearly have shown you that you iterate till 2 instead of 1.
That's only the immediate fix, however. The real fix is to simply never iterate over a datastructure manually. That's what iterators are for.
Something like this, where Ruby will keep track of the index for you:
a.each_with_index do |hsh, i| hsh['language'] = b[i] end
Or, without fiddling with indices at all:
a.zip(b.zip(['language'].cycle).map(&:reverse).map(&Array.method(:[])).map(&:to_h)).map {|x, y| x.merge!(y) }
[Note: this last one doesn't mutate the original Arrays and Hashes unlike the other ones.]
The problem you're having is that your (0..a.length) is inclusive. a.length = 2 so you want to modify it to be 0...a.length which is exclusive.
On a side note, you could use Array#each_with_index like this so you don't have to worry about the length and so on.
a.each_with_index do |hash, index|
hash['language'] = b[index]
end
Here is another method you could use
b.each_with_index.with_object(a) do |(lang,i),obj|
obj[i]["language"] = lang
obj
end
#=>[
{"one"=>1, "two"=>2, "language"=>"english"},
{"uno"=>1, "dos"=>2, "language"=>"spanish"}
]
What this does is creates an Enumerator for b with [element,index] then it calls with_object using a as the object. It then iterates over the Enumerator passing in each language and its index along with the a object. It then uses the index from b to find the proper index in a and adds a language key to the hash that is equal to the language.
Please know this is a destructive method where the objects in a will mutate during the process. You could make it non destructive using with_object(a.map(&:dup)) this will dup the hashes in a and the originals will remain untouched.
All that being said I think YAML would be better suited for a task like this but I am not sure what your constraints are. As an example:
yml = <<YML
-
one: 1
two: 2
language: "english"
-
uno: 1
dos: 2
language: "spanish"
YML
require 'yaml'
YAML.load(yml)
#=>[
{"one"=>1, "two"=>2, "language"=>"english"},
{"uno"=>1, "dos"=>2, "language"=>"spanish"}
]
Although using YAML I would change the structure for numbers to be more like language => Array of numbers by index e.g. {"english" => ["zero","one","two"]}. That way you can can access them like ["english"][0] #=> "zero"

Resources