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

  • SEARCH
  • Home
  • 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 3938928
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:14:26+00:00 2026-05-20T00:14:26+00:00

I have an array like: [4|23, 1, 3|10, 2] I want to get the

  • 0

I have an array like:

["4|23", "1", "3|10", "2"]

I want to get the sum of the odd elements, i.e. 1 + 2 = 3, perhaps with inject?

This is the response by Redis ZRANGE WITHSCORES for a sorted set. Ideally, I want to get the sum of the SCORES in a sorted set.

  • 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-20T00:14:27+00:00Added an answer on May 20, 2026 at 12:14 am

    Thanks to everyone for your answers. All of them were really cool & enlightening.

    I came up with an answer of my own. It’s pretty straightforward:

    sum = 0; gifts.each_with_index { |s, i| sum += s.to_i if i % 2 == 1 }; sum
    

    I did a performance check:

    require "benchmark"
    MANY = 50000    
    gifts = [
      "4|2323", "1",
      "3|102343", "2",
      "0|12330", "1",
      "3|10234873", "2",
      "5|2343225", "1",
      "5|23423744", "1",
      "2|987", "4",
      "0|987345", "1",
      "2|46593", "1",
      "4|78574839", "3",
      "3|4756848578", "1",
      "3|273483", "3"
    ]
    
    Benchmark.bmbm do |x|
      x.report("each_with_index") { MANY.times { sum = 0; gifts.each_with_index { |s, i| sum += s.to_i if i % 2 == 1 }; sum } }
      x.report("each_with_index") { MANY.times { sum = 0; gifts.each_with_index { |s, i| sum += s.to_i if i.odd? }; sum } }
      x.report("values_at") { MANY.times { gifts.values_at(*(1..gifts.length).step(2)).inject(0) { |s, n| s += n.to_i } } }
      x.report("each_slice") { MANY.times { gifts.each_slice(2).inject(0) { |i, (j,k)| i += k.to_i } } }
      x.report("values_at") { MANY.times { gifts.values_at(*gifts.each_index.select { |i| i.odd? }).map(&:to_i).inject(&:+) } }
      x.report("hash") { MANY.times { Hash[*gifts].values.map(&:to_i).reduce(:+) } }
    end
    

    Running the script above output the following on my Mac:

                          user     system      total        real
    each_with_index   0.300000   0.000000   0.300000 (  0.305377)
    each_with_index   0.340000   0.000000   0.340000 (  0.334806)
    values_at         0.370000   0.000000   0.370000 (  0.371520)
    each_slice        0.380000   0.000000   0.380000 (  0.376020)
    values_at         0.540000   0.000000   0.540000 (  0.539633)
    hash              0.560000   0.000000   0.560000 (  0.560519)
    

    Looks like my answer is the fastest. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array like this. What i want is to get the value
if I have an array like this: var myArray:Array = [apple,orange,strawberry]; then I want
I have an array like: [1,2,3,4,5,6,7,8,9] And I want to get items in duets
I have an array like this: int sayilar[10] = {5,6,2,1,4,2,5,5,5,2}; now I want to
I have a numpy array like this a = np.array(1) Now if I want
I have an array of array like below with all numeric values. I want
I have a NumPy array [1,2,3,4,5,6,7,8,9,10,11,12,13,14] and want to have an array structured like
I have an array that looks like $numbers = array('first', 'second', 'third'); I want
I have array like this: $path = array ( [0] => site\projects\terrace_and_balcony\mexico.jpg [1] =>
An array is defined of assumed elements like I have array like String[] strArray

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.