On 10/25/2009 03:57 AM, Matt Beckley wrote:
> Working on code to fill an array with 200 random integer elements. The
> elements range from 1-100 in value. Next I use the .sort method to see
> how many values fall between 1-10 to visually check how many random
> numbers will fall between 1-10. Now I wanna take my array and apply a
> method to it that will check which elements are less 21 but more then
> 10. For you math types 10<x<21. How do I accomplish that with ruby. Do I
> accomplish it by doing it this way or am I overlooking a better method
> for evaluating and comparing the elements in my array? I think I got the
> right method but the block is wrong?
> 
> a = nums1.reject {|x| x<10 && x>21}
> 
> Heres what I have so far of my program:
> ********************************************************************************
> nums = []
> 200.times do
>   nums << rand(100)
> end
> 
> nums1 = nums.sort
> nums1.each {|n| print n, "," }
> 
> ********************************************************************************
> 
> Please include the code so I can try it out on my editor and get it
> figured out!

If I understand your requirement properly you want to sort numeric 
values into buckets.  Here is one way that you can do it, assuming your 
buckets are 10 numbers wide each:

nums ...
buckets = Hash.new 0

nums.each do |x|
   buckets[x - x % 10] += 1
end

buckets.sort.each do |b,count|
   printf "%4d %5d\n", b, count
end

If you have different alignments of buckets the code in the block of 
course becomes more complicated.

Kind regards

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/