-2

I'm returning the index of the smallest integer in an array. I found a solution from this forum. Here is what I did:

require 'amatch'
include Amatch

ingredients_arr = [
  "All purpose", "Ammoniaco", "Assorted sprinkles", "Baking Powder",
  "Baking soda", "Banana", "Banana flavor", "Bread improver", "Brown Sugar",
  "Buko pandan flavor", "Butter", "Butter flavor", "Butter oil subs",
  "Cake Flour", "Cake emulsi\nfier", "Canyon baking powder", "Cheese",
  "Chiffon oil", "Choco flavor", "Choco spri\nnkles", "Cocoa (imp)",
  "Cocoa (loc)", "Coconut", "Condensada", "Cooking\nOil", "\n    Corn Starch",
  "Dessicated", "Dutch choco fudge premium", "Dutch cocoa premium", "Egg",
  "Evaporad\n    a (B)", "Evaporada (S)", "Evaporadaorated\n(B)", "First Class",
  "Food color", "Glucose", "Go\n    ld coin", "Heart sprinkles", "LPG", "Lard",
  "Linga", "Margarine", "Mocha flavor", "Mongo paste red", "Onion", "Ovalet",
  "Powdered sugar", "Rhum", "Royal", "Salt", "Sibuyas", "Skim milk (h-end)",
  "Skim milk (l-end)", "\n    Star sprinkles", "Strawberry flavor",
  "Styro (l. plan)", "Super syrup", "Taba", "Tartar", "\n    Third Class",
  "Ube Paste", "Ube flavor", "Vanilla", "Vanilla 1G", "Vivid icing",
  "Wash Sugar", "Water", "White Sugar"
]
i, ingredient = 1, "Flour"
ing_array = Array.new
until ingredient == ""
  puts "Enter ingredient #{i}: "
  ingredient = gets.chomp
  ing_array << ingredient
  i += 1
end
ing_array.pop
m = Sellers.new("margarine")
no_words = ing_array.length
ing_index_arr = Array.new
i = 0
while i < no_words
  rating_arr = Array.new
  m = Sellers.new(ing_array[i])
  j = 0
  while j < ingredients_arr.length
    x = m.match(ingredients_arr[j])
    rating_arr << x
    j += 1
  end
  y = rating_arr.each.with_index.find_all{|a, i| a == rating_arr.min }.map{|a, b| b}
  ing_index_arr << y
  i += 1
end
ing_index_arr # => [[0], [67], [4]]

but I need something like this:

[0, 67, 4]

Hope someone can help me.

sawa
  • 165,429
  • 45
  • 277
  • 381
Jo Den
  • 23
  • 6
  • 1
    In future, please boil your question down to its essentials. All the code you have given and your sample input are irrelevent. You obtain an array `arr` of arrays (e.g., `[[0],[67],[4]]`), each of the latter containing a single integer. As I understand, your question is how you can convert `arr` to an array containing the integers from the inner arrays (i.e., `[0,67,4]`). How you obtained `arr` is irrelevant. – Cary Swoveland Dec 02 '15 at 04:11
  • I'm sorry Cary, if most of I've written are irrelevant. I'm just thinking that someone might just look at the code and point out how I obtained an array with integers in an array form. I'll ask clearly next time. Thanks for the pointers. – Jo Den Dec 02 '15 at 06:05
  • No need to apologize. You may wish to look through this [faq](http://stackoverflow.com/help) when you have time. – Cary Swoveland Dec 02 '15 at 06:40
  • 1
    I see. If you would like suggestions on how to restructure your code--both to produce the desired result and to make it more "Ruby-like" (sorry), you could edit your question to ask for that, and to supply the additional code that is needed (that in `Amatch`, at least the class definition for `Sellers`). In addition, it would be better to replace the code that constructs `ing_array` from user inputs with an example of what that array might look like (e.g., `ing_array = ["Onion", "Salt",..]`) and then show the array you want as the desired result that corresponds to that value of `ing_array`. – Cary Swoveland Dec 02 '15 at 08:21
  • 1
    Also, don't be too hasty to select an answer. If an answer does not address your actual question, clarify your question with an edit and possibly leave a comment on the answer. Once you've selected an answer readers will naturally conclude that your question is the one that has been answered. Even if the answerer has interpreted your question correctly, selecting an answer may discourage other, possibly better, answers. There's no rush. Many SO members wait at least a couple of hours--sometimes much longer--before applying the green checkmark. – Cary Swoveland Dec 02 '15 at 08:28
  • 1
    Thanks Cary. Actually it's my first time joining in this kind of forum. It even took me half an hour just to post and compose that query because I don't know how everything works. So again thanks for entertaining my question. And of course for the tips. – Jo Den Dec 02 '15 at 08:59

2 Answers2

1

If you want to collapse your sub-arrays, use the flatten method. http://ruby-doc.org/core-2.2.0/Array.html#method-i-flatten

[[0],[67],[4]].flatten == [0,67,4]
philip yoo
  • 2,462
  • 5
  • 22
  • 37
1

I'm not sure if I understood You correctly. If you want to get index of the smalleest integer in the array You can simply sort it.

array=[2,9,90,345]
array.sort
=> [2, 9, 90, 345]

In this case the lowest integer would have always an index=0

When you have array=[2,3,4] it is already numeric:

array[0].class
=> Fixnum

  • I can't sort it because what I need is the given position of the smallest value since it will be used to extract the corresponding element in another array. Anyway, I have discovered that the values from 'y = rating_arr.each.with_index.find_all{|a, i| a == rating_arr.min }.map{|a, b| b}' is an array. So what I've been doing is wrong, plugging an array inside an array. Thanks for your time! – Jo Den Dec 02 '15 at 06:12
  • Jo, that will do it. You could also write, `min_rating = rating_arr.min; rating_arr.each_index.select { |i| rating_arr[I] = min_rating }`. `Array#select`. has the same effect as `Enumerable#find_all` (aka `Enumerable#select`). – Cary Swoveland Dec 02 '15 at 09:41