Page 1 of 1

Generate Array?

PostPosted: Sat Nov 16, 2019 11:11 pm
by guyman
Ok last question for the day.
What's the best way to generate/calculate an array from scratch, in consistent relation to a function from last entry, or the index # ?
so just..
Designating # of x bins...
entering function (ie x=x+3... or x=index ..or x=index*2+3/4^179)

I'm going for pretty big arrays.....

Re: Generate Array?

PostPosted: Sun Nov 17, 2019 12:33 am
by RJHollins
Hi Guy ...

not sure exactly what your looking for ... but posted sometime back, TROG had put together a package of modules called 'TROGS TOOLZ'.

In the 'Auto Numbering folder is an AutoFunction Generator that can generate a variety of Arrays and functions.

not sure if this the latest ... but maybe can help:

Re: Generate Array?

PostPosted: Sun Nov 17, 2019 1:14 am
by tulamide
guyman wrote:What's the best way to generate/calculate an array from scratch, in consistent relation to a function from last entry, or the index # ?

If you know the size of the array beforehand, the best way is to use its built in method #new:

Code: Select all
my_array = Array.new(20) { |index| index*2+3/4^179 }
## creates an array with 20 entries calculated from the code block {}


if you don't know the size beforehand you have to append to an existing array, for example:

Code: Select all
my_array = []
index = 0
my_array << index*2+3/4^179


This will make you happy: https://ruby-doc.org/core-1.9.3/index.html

Re: Generate Array?

PostPosted: Sun Nov 17, 2019 3:55 pm
by martinvicanek
Some options for array creation are: :mrgreen:

Re: Generate Array?

PostPosted: Sun Nov 17, 2019 4:35 pm
by tulamide
martinvicanek wrote:...
It does, Martin, it does. But for a good reason. It's not "the best" way as asked, it's the worst way, because it's not the Ruby way. A for loop is one of the worst methods, especially when doing large arrays, as the op said.

But the other methods impress me! I would never had thought about using ASM, that's amazing!

Re: Generate Array?

PostPosted: Fri Nov 22, 2019 7:58 pm
by wlangfor@uoguelph.ca
i like to build an array, as long as you're sure the index number is accurate there really is no drawback unlike javascript or php.