The question I have is how to load a hash where the key is not numeric
or does not follow a cycle. I’m doing an exercise where I have to go
asking people’s names and their corresponding heights, these the income
each in a different array, and then deposit them into a hash and get
over 2 meters, 1.60 meters minors, etc. The problem is how to charge the
hash data arrays. Here is the code:
so the question in your other thread has been answered?
The problem in this case, again, is that you somehow don’t see the
obvious solution but instead do all kinds of complicated things with
nested loops and such.
Why do you need the intermediate arrays when you could simply store the
value in the hash? Also be careful with variable naming. There is no
“alturas”. Just stick with English.
So dump the arrays and loops and simply save the height directly in the
hash with the name as the key:
heights = {}
…
name = gets.chomp
height = gets.chomp
heights[name] = height
That’s it.
If you do want to use the arrays for whatever reason, you mustn’t use a
nested loop. This will give you each combination of a name and a height.
But what you actually want is the first element of each array, then the
second etc. In other words: You must iterate over the arrays parallely.
Like this, for example:
a.each_with_index do |name, i| # pass an index for each element
The question I have is how to load a hash where the key is not numeric
or does not follow a cycle. I’m doing an exercise where I have to go
asking people’s names and their corresponding heights, these the income
each in a different array, and then deposit them into a hash and get
over 2 meters, 1.60 meters minors, etc. The problem is how to charge the
hash data arrays. Here is the code:
The program does not do any calculations. What are you trying to
achieve? Do you want to ouput all names of people who are less than
1.6m and over 2m? That would be a selection of the data which I
cannot see in your program.
Just keep in mind that all (programming) languages are different, and
the
solutions to a single problem must necessarily be different in those
languages.
Writing a java program using code that works in C is just as “wrong.”
It just so happens that ruby provides a much more “functional” standard
library, so where creating or analysing a java-esque solution will help
your understanding for similar languages (C++, C#, etc.) you’d still
need
to come up with a different algorithmic approach to solve it in a purely
procedural language like assembly, and yet another for a more functional
language like ruby.
is not applicable in Ruby using for loops, but not the only language I
use then I get a little tricky to get used.
not if you understand the problem.
You lost me here completely. What are you trying to say?
Kind regards
robert
What I’m trying to say is that whenever I post some code, which use a
conventional structure, such as a for loop, a while loop, etc, I
responded
to my something like this is Ruby and is not C, C++ or Java, and
some works that utilize own Ruby (ruby way), the problem is that these
functions only work in ruby and whenever you use another language, I
have to change the way I program certain functions.
language like assembly, and yet another for a more functional language like
ruby.
Absolutely agree! Joao, also consider this: if all programming
languages would be usable in the same way there would not be different
programming languages at all. The whole point of having so many of
them is that they all have a different set of features which make
solving some types of problems easier than others. There is simply no
programming language which fits all kinds of problems equally well.
you better get used to the specific ways to solve problems in the
languages you use. Please do not expect all approaches to work in all
languages equally well. You’re not using a saw and a drill the same
way, do you?
Kind regards
robert
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.