[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

array.include?

jabowen

7/30/2007 4:18:00 PM

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...

9 Answers

Dan Stevens (IAmAI)

7/30/2007 4:21:00 PM

0

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

On 30/07/07, Jeffrey Bowen <ja_bowen@yahoo.com> wrote:
> 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...
>
>

Brett Simmers

7/30/2007 5:16:00 PM

0

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

Jeffrey Bowen wrote:
> 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...
>
>
>

Thomas Wieczorek

7/30/2007 5:20:00 PM

0

2007/7/30, Jeffrey Bowen <ja_bowen@yahoo.com>:
> 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_

Robert Klemme

7/30/2007 9:01:00 PM

0

On 30.07.2007 19:16, Brett Simmers wrote:
> 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.

Gordon Thiesfeld

7/30/2007 10:08:00 PM

0

On Jul 30, 11:17 am, Jeffrey Bowen <ja_bo...@yahoo.com> wrote:
> 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.

Tim Hunter

7/30/2007 10:23:00 PM

0

Gordon Thiesfeld wrote:
> On Jul 30, 11:17 am, Jeffrey Bowen <ja_bo...@yahoo.com> wrote:
>
>> 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.
>
>
>
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.

--
RMagick OS X Installer [http://rubyforge.org/project...]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?for...]
RMagick Installation FAQ [http://rmagick.rubyforge.org/instal...]


Bertram Scharpf

7/30/2007 10:46:00 PM

0

Hi,

Am Dienstag, 31. Jul 2007, 07:22:48 +0900 schrieb Tim Hunter:
> >On Jul 30, 11:17 am, Jeffrey Bowen <ja_bo...@yahoo.com> wrote:
> >
> >>if @status_array.include? @status_number
> >>else
> >> @status_array << @status_number
> >>end
> >>
> >
> 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


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...

Michael Hollins

7/30/2007 11:19:00 PM

0

Jeffrey Bowen wrote:
> 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?

The simplest rewrite of your code is:

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

Michael Hollins

7/31/2007 2:18:00 AM

0

Michael Hollins wrote:
> Jeffrey Bowen wrote:
>> 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?
>
> The simplest rewrite of your code is:
>
> if !@status_array.include? @status_number
> @status_array << @status_number
> end

Apologies for duplicating other replies. For some reason I didn't notice them before sending my reply.