Loop over an array that changes the elements

Discussion in 'Ruby' started by Bernd Burnt, Jul 13, 2007.

  1. Bernd Burnt

    Bernd Burnt Guest

    Hi,

    I have a rather simple question. I have an array and want to change the
    elements of the array in a loop. For example in Java, you would write:

    int[] numbers = {1, 2, 3}

    for(int i = 0; i < numbers.length; i++)
    {
    numbers = numbers * 2
    }


    In Ruby the common way to loop over an array is Array#each

    But

    numbers = [1, 2, 3]

    numbers.each do |num|
    num = num * 2
    end

    would not change the array itself.

    Of course, you can write something like

    for i in 0 .. numbers.length-1 do
    numbers = numbers * 2
    end

    But this in my eyes is not really the Ruby way. Is there a standard way
    of doing this?

    Thx a lot!

    --
    Posted via http://www.ruby-forum.com/.
     
    Bernd Burnt, Jul 13, 2007
    #1
    1. Advertising

  2. Bernd Burnt

    Robert Dober Guest

    On 7/13/07, Bernd Burnt <> wrote:
    > Hi,
    >
    > I have a rather simple question. I have an array and want to change the
    > elements of the array in a loop. For example in Java, you would write:
    >
    > int[] numbers = {1, 2, 3}
    >
    > for(int i = 0; i < numbers.length; i++)
    > {
    > numbers = numbers * 2
    > }
    >

    numbers.map!{ |x| x*2 }
    HTH
    Robert
    >
    > In Ruby the common way to loop over an array is Array#each
    >
    > But
    >
    > numbers = [1, 2, 3]
    >
    > numbers.each do |num|
    > num = num * 2
    > end
    >
    > would not change the array itself.

    No it would point num to a new Integer simply, you can only change the
    objects num refers to, but in our case as these objects are immutable
    Integers it wont work.
    Consider Strings which are muteable in ruby, and try this code ( best in irb )
    letters=%w{a b c}
    letters.each do | letter |
    letter << "*"
    end

    Still I prefer
    letters.map{ |l| l << "*" }
    although
    letters.map!{ |l| l + "*" }
    is probably much cleaner code :)

    HTH
    Robert
    >
    > Of course, you can write something like
    >
    > for i in 0 .. numbers.length-1 do
    > numbers = numbers * 2
    > end


    >
    > But this in my eyes is not really the Ruby way. Is there a standard way
    > of doing this?

    Well spotted Bernd ;)
    >
    > Thx a lot!
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >


    HTH
    Robert
    --
    I always knew that one day Smalltalk would replace Java.
    I just didn't know it would be called Ruby
    -- Kent Beck
     
    Robert Dober, Jul 13, 2007
    #2
    1. Advertising

  3. Bernd Burnt

    Guest

    > Hi,
    >
    > I have a rather simple question. I have an array and want to change the
    > elements of the array in a loop. For example in Java, you would write:
    >
    > int[] numbers = {1, 2, 3}
    >
    > for(int i = 0; i < numbers.length; i++)
    > {
    > numbers = numbers * 2
    > }
    >
    >
    > In Ruby the common way to loop over an array is Array#each
    >
    > But
    >
    > numbers = [1, 2, 3]
    >
    > numbers.each do |num|
    > num = num * 2
    > end
    >
    > would not change the array itself.
    >
    > Of course, you can write something like
    >
    > for i in 0 .. numbers.length-1 do
    > numbers = numbers * 2
    > end
    >
    > But this in my eyes is not really the Ruby way. Is there a standard way
    > of doing this?


    numbers = numbers.map {|number| numer * 2}

    Cheers,
    B
     
    , Jul 13, 2007
    #3
  4. Bernd Burnt

    Guest

    > Hi,
    >
    > I have a rather simple question. I have an array and want to change the
    > elements of the array in a loop. For example in Java, you would write:
    >
    > int[] numbers = {1, 2, 3}
    >
    > for(int i = 0; i < numbers.length; i++)
    > {
    > numbers = numbers * 2
    > }
    >
    >
    > In Ruby the common way to loop over an array is Array#each
    >
    > But
    >
    > numbers = [1, 2, 3]
    >
    > numbers.each do |num|
    > num = num * 2
    > end
    >
    > would not change the array itself.


    You're right.

    As others and I have said, the map solution is good here.

    numbers = numbers.map {|number| number*2}

    If you really wanted to change the array itself though, you could do this:

    numbers.each_index {|i| numbers = numbers * 2}

    or

    numbers.each_index {|i| numbers *= 2}

    Thanks,
    Benj
     
    , Jul 13, 2007
    #4
  5. Bernd Burnt

    Xavier Noria Guest

    El Jul 13, 2007, a las 2:53 PM, escribi=F3:

    > numbers =3D numbers.map {|number| numer * 2}


    Even, to be closer to the in-place editing,

    numbers.map! {|n| n*2}

    -- fxn
     
    Xavier Noria, Jul 13, 2007
    #5
  6. Bernd Burnt

    Robert Dober Guest

    On 7/13/07, <> wrote:
    > > Hi,
    > >
    > > I have a rather simple question. I have an array and want to change the
    > > elements of the array in a loop. For example in Java, you would write:
    > >
    > > int[] numbers = {1, 2, 3}
    > >
    > > for(int i = 0; i < numbers.length; i++)
    > > {
    > > numbers = numbers * 2
    > > }
    > >
    > >
    > > In Ruby the common way to loop over an array is Array#each
    > >
    > > But
    > >
    > > numbers = [1, 2, 3]
    > >
    > > numbers.each do |num|
    > > num = num * 2
    > > end
    > >
    > > would not change the array itself.
    > >
    > > Of course, you can write something like
    > >
    > > for i in 0 .. numbers.length-1 do
    > > numbers = numbers * 2
    > > end
    > >
    > > But this in my eyes is not really the Ruby way. Is there a standard way
    > > of doing this?

    >
    > numbers = numbers.map {|number| numer * 2}

    Hmm what about the GC?
    >
    > Cheers,
    > B
    >
    >
    >



    --
    I always knew that one day Smalltalk would replace Java.
    I just didn't know it would be called Ruby
    -- Kent Beck
     
    Robert Dober, Jul 13, 2007
    #6
  7. writes:

    > As others and I have said, the map solution is good here.
    >
    > numbers = numbers.map {|number| number*2}
    >
    > If you really wanted to change the array itself though, you could do this:
    >
    > numbers.each_index {|i| numbers = numbers * 2}


    Actually, as I think someone already posted, map! is probably the most
    idiomatic way of doing this:

    numbers.map! {|x| 2*x}

    --
    s=%q( Daniel Martin --
    puts "s=%q(#{s})",s.to_a.last )
    puts "s=%q(#{s})",s.to_a.last
     
    Daniel Martin, Jul 13, 2007
    #7
  8. Bernd Burnt

    Robert Dober Guest

    On 7/13/07, Daniel Martin <> wrote:
    <snip>
    >
    > Actually, as I think someone already posted, map! is probably the most
    > idiomatic way of doing this:


    Daniel this is great I am *not* in your killfile :)

    Robert
    --
    I always knew that one day Smalltalk would replace Java.
    I just didn't know it would be called Ruby
    -- Kent Beck
     
    Robert Dober, Jul 13, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hicham Mouline
    Replies:
    1
    Views:
    395
    Kai-Uwe Bux
    Apr 11, 2010
  2. Joe Buck
    Replies:
    11
    Views:
    190
    Robert Klemme
    Jan 2, 2010
  3. it_says_BALLS_on_your forehead

    Iterating over all elements in a 3D array

    it_says_BALLS_on_your forehead, Mar 27, 2007, in forum: Perl Misc
    Replies:
    5
    Views:
    135
    nikolas pontikos
    Mar 27, 2007
  4. Replies:
    4
    Views:
    189
  5. Isaac Won
    Replies:
    9
    Views:
    384
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page