array.include?

J

Jeffrey Bowen

if @status_array.include? @status_number
else
@status_array << @status_number
end

I'm using the above to append an array if it does not
include @status_number. Is there an include?-not
function that would eliminate the else? Or another
way to do the same thing?

Jeff



____________________________________________________________________________________
Got a little couch potato?
Check out fun summer activities for kids.
http://search.yahoo.com/search?fr=oni_on_mail&p=summer+activities+for+kids&cs=bz
 
D

Dan Stevens (IAmAI)

@status_array << @status_number unless @status_array.include? @status_number
 
B

Brett Simmers

Do you need the order of the items to be preserved? If not, you should
think about using a Set instead of an Array. You could then use
Set#add, which accomplishes the same thing and is MUCH faster,
especially if your data set gets fairly large. Array#include? has to do
a linear search over the entire Array, but most operations on Sets are
constant time, including Set#add, Set#add?, and Set#include?

Brett
 
T

Thomas Wieczorek

2007/7/30 said:
if @status_array.include? @status_number
else
@status_array << @status_number
end

Is there an include?-not > function that would eliminate the else?
You can use _not_ in the if statement:
if not @status_array.include? @status_number
@status_array << @status_number
end

or

@status_array << @status_number if not @status_array.include? @status_number

or like Dan did it with _unless_
 
R

Robert Klemme

Do you need the order of the items to be preserved? If not, you should
think about using a Set instead of an Array. You could then use
Set#add, which accomplishes the same thing and is MUCH faster,
especially if your data set gets fairly large. Array#include? has to do
a linear search over the entire Array, but most operations on Sets are
constant time, including Set#add, Set#add?, and Set#include?

I'd also say that a Set seems most appropriate here (a Hash might as
well, depending on what has to be done with the data). And you can even
use << with a Set.

Kind regards

robert


PS: Please do not top post.
 
G

Gordon Thiesfeld

if @status_array.include? @status_number
else
@status_array << @status_number
end

How about not checking at all? Just put them all in there and then
use @status_array.uniq! when necessary? I'm not advocating this.
Just curious what people think.
 
T

Tim Hunter

Gordon said:
How about not checking at all? Just put them all in there and then
use @status_array.uniq! when necessary? I'm not advocating this.
Just curious what people think.
If it was me, I'd just use a hash instead of an array, with
@status_number as the key and anything (1, for example) as the value.
Let the hash object figure out if @status_number is already a key. It'll
be fast, and any time you need a list of status_numbers, just call
hash.keys.
 
B

Bertram Scharpf

Hi,

Am Dienstag, 31. Jul 2007, 07:22:48 +0900 schrieb Tim Hunter:
If it was me, I'd just use a hash instead of an array, with
@status_number as the key and anything (1, for example) as the value.
Let the hash object figure out if @status_number is already a key. It'll
be fast, and any time you need a list of status_numbers, just call
hash.keys.

Probably it is useful to count them.

@status_hash = Hash.new 0
...
@status_hash[ status_number] += 1

Bertram
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,264
Messages
2,571,314
Members
47,990
Latest member
MauricioEl

Latest Threads

Top