Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7189671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:21:05+00:00 2026-05-28T19:21:05+00:00

I need to know how you break an array into sub-arrays based on the

  • 0

I need to know how you break an array into sub-arrays based on the value of the integers in the array. What I’m trying to do is take a large array and break it into 1-10, 11-20, 21-30…etc and I need to then be able to count those sub arrays so I have numbers that are like “7 integers between 1-10, 6 integers between 11-20, 12 integers between 21-30.”

I’ve got a single line random number generator that will give me my array like the following:

rand_num = (Array.new(200) {(1..100).to_a[rand(100)]})

This gives me my array of 200 hundred random numbers between 1 and 100 and now I need to be able to split them apart according to value and tell how much is in each one.

Then I need it to display those numbers. I’ve searched everywhere and I want to say you do this with .partition, but I just can’t get it to work.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T19:21:07+00:00Added an answer on May 28, 2026 at 7:21 pm

    If your buckets really are that simple then you could use group_by like this:

    array.group_by { |n| (n - 1) / 10 }
    

    That will give you Hash like this:

    {0=>[1, 2, 3, ...], 1=>[11, 12, 13, ...], ...}
    

    then you just throw in a sort_by to force a nice ordering and a map to summarize the results:

    array.group_by { |n| (n - 1) / 10 }.
          sort_by { |k, v| k }.
          map { |n, a| [10 * n + 1 .. 10 * (n + 1), a.length] }
    

    For example:

    # The %7 is just an easy way to get a non-uniform set.
    >> (1..100).select { |n| n % 7 == 0 }
    => [7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84, 91, 98]
    >> (1..100).select { |n| n % 7 == 0 }.sort_by { |k, v| k }.group_by { |n| (n - 1) / 10 }.map { |n, a| [10 * n + 1 .. 10 * (n + 1), a.length] }
    => [[1..10, 1], [11..20, 1], [21..30, 2], [31..40, 1], [41..50, 2], [51..60, 1], [61..70, 2], [71..80, 1], [81..90, 1], [91..100, 2]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need help with creating a regex for putting all values into an array!
I need to transfer the values from a PHP array into a JavaScript array
I have two questions: 1. How to break a string into an array given
Need to know this so that i could send DTMF and that is going
I need to know about Epoll On linux System. Could you recommend manual or
I need to know how the performance of different XML tools (parsers, validators, XPath
I need to know how much space occupies all the databases inside an SQL
I need to know how to turn on Code Coverage when running TFS builds
I need to know when the memory will be allocated for a particular program.
I need to know, from within Powershell, if the current drive is a mapped

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.