Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Loop over an array that changes the elements

Reply
Thread Tools

Loop over an array that changes the elements

 
 
Bernd Burnt
Guest
Posts: n/a
 
      07-13-2007
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[i] = numbers[i] * 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[i] = 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/.

 
Reply With Quote
 
 
 
 
Robert Dober
Guest
Posts: n/a
 
      07-13-2007
On 7/13/07, Bernd Burnt <(E-Mail Removed)> 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[i] = numbers[i] * 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[i] = 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

 
Reply With Quote
 
 
 
 
benjohn@fysh.org
Guest
Posts: n/a
 
      07-13-2007
> 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[i] = numbers[i] * 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[i] = 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


 
Reply With Quote
 
benjohn@fysh.org
Guest
Posts: n/a
 
      07-13-2007
> 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[i] = numbers[i] * 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[i] = numbers[i] * 2}

or

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

Thanks,
Benj


 
Reply With Quote
 
Xavier Noria
Guest
Posts: n/a
 
      07-13-2007
El Jul 13, 2007, a las 2:53 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) escribi=F3:

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


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

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

-- fxn


 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      07-13-2007
On 7/13/07, (E-Mail Removed) <(E-Mail Removed)> 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[i] = numbers[i] * 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[i] = 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

 
Reply With Quote
 
Daniel Martin
Guest
Posts: n/a
 
      07-13-2007
(E-Mail Removed) 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[i] = numbers[i] * 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 -- (E-Mail Removed)
puts "s=%q(#{s})",s.to_a.last )
puts "s=%q(#{s})",s.to_a.last

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      07-13-2007
On 7/13/07, Daniel Martin <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
number of distinct elements in a const container and iterating over the distinct elements Hicham Mouline C++ 1 04-11-2010 10:56 AM
VOIP over VPN over TCP over WAP over 3G Theo Markettos UK VOIP 2 02-14-2008 03:27 PM
newb: recurse over elements children and disable all form elements SteveKlett@gmail.com Javascript 4 09-20-2006 07:42 AM



Advertisments