[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

array element access

Tomas Fischer

3/12/2006 8:35:00 PM

Hi,

I've got an array a= [1,2,3,4,5,6] and want to access each second
element:
b=[2,4,6]. How is this done in ruby?

I know, that I can use a for-loob and modulo operator, but I think there
is a "ruby way" of doing this.

Thanks.
tomas

--
Posted via http://www.ruby-....


14 Answers

Daniel Harple

3/12/2006 8:54:00 PM

0


On Mar 12, 2006, at 9:34 PM, Tomas Fischer wrote:

> Hi,
>
> I've got an array a= [1,2,3,4,5,6] and want to access each second
> element:
> b=[2,4,6]. How is this done in ruby?
>
> I know, that I can use a for-loob and modulo operator, but I think
> there
> is a "ruby way" of doing this.
>
> Thanks.
> tomas
>
> --
> Posted via http://www.ruby-....
>

require 'enumerator'
a = (1..10).to_a
p a.enum_for(:each_slice, 2).collect { |m| m[1] } # -> [2, 4, 6, 8, 10]

-- Daniel


Harold Hausman

3/12/2006 9:05:00 PM

0

> I've got an array a= [1,2,3,4,5,6] and want to access each second
> element:
> b=[2,4,6]. How is this done in ruby?
>

# This is ugly, but it works:

a = [1,2,3,4,5,6]
(1..a.length).step(2) { |i| puts a[i] }

# -Harold


Tim Hunter

3/12/2006 9:20:00 PM

0

Daniel Harple wrote:
>
> On Mar 12, 2006, at 9:34 PM, Tomas Fischer wrote:
>
>> Hi,
>>
>> I've got an array a= [1,2,3,4,5,6] and want to access each second
>> element:
>> b=[2,4,6]. How is this done in ruby?
>>
>> I know, that I can use a for-loob and modulo operator, but I think there
>> is a "ruby way" of doing this.
>>
>> Thanks.
>> tomas
>>
>> --
>> Posted via http://www.ruby-....
>>
>
> require 'enumerator'
> a = (1..10).to_a
> p a.enum_for(:each_slice, 2).collect { |m| m[1] } # -> [2, 4, 6, 8, 10]
>
> -- Daniel
>
>
enumerator is handy but it seems a bit like overkill for a task that can
be done with the builtin classes:

irb(main):009:0> ary = [1,2,3,4,5,6]
=> [1, 2, 3, 4, 5, 6]
irb(main):010:0> ary2 = []
=> []
irb(main):011:0> ary.each_with_index { |a, i| ary2 << a if i % 2 == 1 }
=> [1, 2, 3, 4, 5, 6]
irb(main):012:0> ary2
=> [2, 4, 6]

Farrel Lifson

3/12/2006 9:46:00 PM

0

array.inject(false) do |alternate,element|
if alternate
# Your processing
end
!alternate
end

On 3/12/06, Tomas Fischer <tomas_fischer99_spamremoveit_@yahoo.com> wrote:
> Hi,
>
> I've got an array a= [1,2,3,4,5,6] and want to access each second
> element:
> b=[2,4,6]. How is this done in ruby?
>
> I know, that I can use a for-loob and modulo operator, but I think there
> is a "ruby way" of doing this.
>
> Thanks.
> tomas
>
> --
> Posted via http://www.ruby-....
>
>


Hans Fugal

3/12/2006 10:08:00 PM

0

Daniel Harple wrote:
>
> On Mar 12, 2006, at 9:34 PM, Tomas Fischer wrote:
>
>> Hi,
>>
>> I've got an array a= [1,2,3,4,5,6] and want to access each second
>> element:
>> b=[2,4,6]. How is this done in ruby?
>>
>> I know, that I can use a for-loob and modulo operator, but I think there
>> is a "ruby way" of doing this.
>>
>> Thanks.
>> tomas
>>
>> --Posted via http://www.ruby-....
>>
>
> require 'enumerator'
> a = (1..10).to_a
> p a.enum_for(:each_slice, 2).collect { |m| m[1] } # -> [2, 4, 6, 8, 10]
>
> -- Daniel
>
>

I might do it this way:

map , = (1..10).partition{|i| i%2==0}
map.each {|i| puts a[i]}

or this way

a.each_with_index {|a,i| if i%2 == 0; ... ; end}

Depending on my needs and mood

William James

3/12/2006 10:16:00 PM

0

Daniel Harple wrote:
> On Mar 12, 2006, at 9:34 PM, Tomas Fischer wrote:
>
> > Hi,
> >
> > I've got an array a= [1,2,3,4,5,6] and want to access each second
> > element:
> > b=[2,4,6]. How is this done in ruby?
> >
> > I know, that I can use a for-loob and modulo operator, but I think
> > there
> > is a "ruby way" of doing this.
> >
> > Thanks.
> > tomas
> >
> > --
> > Posted via http://www.ruby-....
> >
>
> require 'enumerator'
> a = (1..10).to_a
> p a.enum_for(:each_slice, 2).collect { |m| m[1] } # -> [2, 4, 6, 8, 10]
>
> -- Daniel

f=1
[1,2,3,4,5,6].select{f=!f}.each{|x|p x}

Luc Heinrich

3/12/2006 11:32:00 PM

0

On 12 mars 06, at 21:34, Tomas Fischer wrote:

> I've got an array a= [1,2,3,4,5,6] and want to access each second
> element:
> b=[2,4,6]. How is this done in ruby?

irb(main):001:0> a = [1,2,3,4,5,6]
=> [1, 2, 3, 4, 5, 6]
irb(main):002:0> a.select {|e| a.index(e) % 2 == 1 }
=> [2, 4, 6]

--
Luc Heinrich - luc@honk-honk.com - http://www.hon...




Ara.T.Howard

3/12/2006 11:51:00 PM

0

Adam Shelly

3/13/2006 12:01:00 AM

0

On 3/12/06, Luc Heinrich <luc@honk-honk.com> wrote:
> On 12 mars 06, at 21:34, Tomas Fischer wrote:
>
> > I've got an array a= [1,2,3,4,5,6] and want to access each second
> > element:
> > b=[2,4,6]. How is this done in ruby?
>
> irb(main):001:0> a = [1,2,3,4,5,6]
> => [1, 2, 3, 4, 5, 6]
> irb(main):002:0> a.select {|e| a.index(e) % 2 == 1 }
> => [2, 4, 6]
>
> --

That's a problem if there are duplicates in the array:

irb(main):012:0> a=[1,1,2,2,3,3,4,5]
=> [1, 1, 2, 2, 3, 3, 4, 5]
irb(main):013:0> a.select{|e| a.index(e) %2 == 1}
=> [5]
irb(main):014:0> s=true
=> true
irb(main):015:0> a.select{s=!s}
=> [1, 2, 3, 5]
irb(main):016:0>


-Adam


Luc Heinrich

3/13/2006 8:59:00 AM

0

On 13 mars 06, at 01:00, Adam Shelly wrote:

> That's a problem if there are duplicates in the array:

Yep, good catch.

--
Luc Heinrich - luc@honk-honk.com - http://www.hon...