Array.new question

Hello,

1)
I would like to create an array of n empty arrays, i.e.

[ [], [], ... n-3 []'s, [] ]

So that the inner arrays are different objects.

Array.new(n,[]) does not help since the created arrays are not different objects.

Is there an idiomatic way to accomplish this?

2) Is there an idiomatic way to do this (in a generic way, of course):

some_func(
[1,2,3]
[4,5,6]
[7,8,9] )

=> [ [1,4,7], [2,5,8], [3,6,9] ]

Thanks,
Peter
http://www.rubyrailways.com

Peter Szinek wrote:

Hello,

1)
I would like to create an array of n empty arrays, i.e.

[ , , ... n-3 's, ]

So that the inner arrays are different objects.

Array.new(n,) does not help since the created arrays are not different objects.

Is there an idiomatic way to accomplish this?

a = (0..3).collect{} # => [, , , ]
a[0][0] = 1
a # => [[1], , , ]

2) Is there an idiomatic way to do this (in a generic way, of course):

some_func(
[1,2,3]
[4,5,6]
[7,8,9] )

=> [ [1,4,7], [2,5,8], [3,6,9] ]

[1,2,3].zip([4,5,6],[7,8,9]) # => [[1,4,7], [2,5,8], [3,6,9]]

···

--
Alex

Hello,

1)
I would like to create an array of n empty arrays, i.e.

[ , , ... n-3 's, ]

Nicely answered

2) Is there an idiomatic way to do this (in a generic way, of course):

some_func(
[1,2,3]
[4,5,6]
[7,8,9] )

=> [ [1,4,7], [2,5,8], [3,6,9] ]

I suppose you forgot the "," if you forgot the enclosing "[", "]" just
remove the splash operator (*) below:

some_func( *[*0..n-1].map{|line| [*n*line+1..n*line+n] } )

It is quit ugly maybe you should seek for less concentrated code though :wink:
The parser is doing a great job here, I am really abusing it :frowning:

HTH
Robert

Thanks,

···

On 10/17/06, Peter Szinek <peter@rubyrailways.com> wrote:

Peter
http://www.rubyrailways.com

--
The reasonable man adapts himself to the world; the unreasonable one
persists in trying to adapt the world to himself. Therefore all progress
depends on the unreasonable man.

- George Bernhard Shaw

a = (0..3).collect{} # => [, , , ]
a[0][0] = 1
a # => [[1], , , ]

Thx!

2) Is there an idiomatic way to do this (in a generic way, of course):

some_func(
[1,2,3]
[4,5,6]
[7,8,9] )

=> [ [1,4,7], [2,5,8], [3,6,9] ]

[1,2,3].zip([4,5,6],[7,8,9]) # => [[1,4,7], [2,5,8], [3,6,9]]

Sorry, I did not describe the scenario properly.
The thing is that I am iterating over an array (of arrays)
and I don't have the all the arrays at once (the inner arrays are generated on the fly). Let me illustrate it:

input = [ [1,2,3], [4,5,6], [7,8,9] ]
result =

input.each { |i| do_something_with(result, i) }

and the result should be

[ [1,4,7], [2,5,8], [3,6,9] ]

The question sounds: how is do_something_with implemented?
I have already implemented it but I don't think so it's a state-of-the art solution :wink:

Thanks,
Peter
http://www.rubyrailways.com

I think this will do the same thing, too:

a = Array.new(4) { }
# => [, , , ]

a[0][0] = 1
# => 1

a
# => [[1], , , ]

···

On Tue, 2006-10-17 at 18:08 +0900, Alex Young wrote:

Peter Szinek wrote:
> Hello,
>
> 1)
> I would like to create an array of n empty arrays, i.e.
>
> [ , , ... n-3 's, ]
>
> So that the inner arrays are different objects.
>
> Array.new(n,) does not help since the created arrays are not different
> objects.
>
> Is there an idiomatic way to accomplish this?
a = (0..3).collect{} # => [, , , ]
a[0][0] = 1
a # => [[1], , , ]

--
Ross Bamford - rosco@roscopeco.REMOVE.co.uk

Peter Szinek wrote:

a = (0..3).collect{} # => [, , , ]
a[0][0] = 1
a # => [[1], , , ]

Thx!

2) Is there an idiomatic way to do this (in a generic way, of course):

some_func(
[1,2,3]
[4,5,6]
[7,8,9] )

=> [ [1,4,7], [2,5,8], [3,6,9] ]

[1,2,3].zip([4,5,6],[7,8,9]) # => [[1,4,7], [2,5,8], [3,6,9]]

Sorry, I did not describe the scenario properly.
The thing is that I am iterating over an array (of arrays)
and I don't have the all the arrays at once (the inner arrays are generated on the fly). Let me illustrate it:

input = [ [1,2,3], [4,5,6], [7,8,9] ]
result =

input.each { |i| do_something_with(result, i) }

and the result should be

[ [1,4,7], [2,5,8], [3,6,9] ]

The question sounds: how is do_something_with implemented?
I have already implemented it but I don't think so it's a state-of-the art solution :wink:

Right... In that case, how about this:

input.each{|i| i.each_with_index{|x,j| (result[j] ||= ) << x}}

Not sure if it'll play the way you want with variable-length arrays, but it puts the right contents in result.

···

--
Alex

input = [ [1,2,3], [4,5,6], [7,8,9] ]
result =

input.each { |i| do_something_with(result, i) }

and the result should be

[ [1,4,7], [2,5,8], [3,6,9] ]

The question sounds: how is do_something_with implemented?
I have already implemented it but I don't think so it's a state-of-the
art solution :wink:

Not so state-of-the-art but succint:

def do_something_with_result(a, i)
  i.each_with_index { |e, j|
    a[j] ? a[j] << e : a << [e]
  }
end

Alex,

Right... In that case, how about this:

input.each{|i| i.each_with_index{|x,j| (result[j] ||= ) << x}}

Not sure if it'll play the way you want with variable-length arrays, but it puts the right contents in result.

Thx for the solution! Mine was totally the same (for the first time in history since I am using Ruby :slight_smile: as someone proposed on this list :wink:

Peter
http://www.rubyrailways.com

Alex,

> Right... In that case, how about this:
>
> input.each{|i| i.each_with_index{|x,j| (result[j] ||= ) << x}}
>
> Not sure if it'll play the way you want with variable-length arrays, but
> it puts the right contents in result.

Thx for the solution! Mine was totally the same (for the first time in
history since I am using Ruby :slight_smile: as someone proposed on this list :wink:

You are simply brilliant :wink:

Peter

···

On 10/17/06, Peter Szinek <peter@rubyrailways.com> wrote:

http://www.rubyrailways.com

--
The reasonable man adapts himself to the world; the unreasonable one
persists in trying to adapt the world to himself. Therefore all progress
depends on the unreasonable man.

- George Bernhard Shaw

Robert Dober wrote:

···

On 10/17/06, Peter Szinek <peter@rubyrailways.com> wrote:

You are simply brilliant :wink:

I am certainly very far from that... I was just happy that (still being a noob) I have solved something on my own for the first time. What's wrong with that?

Peter
http://www.rubyrailways.com

Peter Szinek schrieb:

Robert Dober wrote:

You are simply brilliant :wink:

I am certainly very far from that... I was just happy that (still being
a noob) I have solved something on my own for the first time. What's
wrong with that?

Certainly nothing,

but i would like to add my own version:

input.each{|i| result << i}
result = result.transpose

cheers

Simon

···

On 10/17/06, Peter Szinek <peter@rubyrailways.com> wrote:

Robert Dober wrote:

> You are simply brilliant :wink:

I am certainly very far from that... I was just happy that (still being
a noob) I have solved something on my own for the first time. What's
wrong with that?

Did you see the smiley Peter? I meant that somebody coming up with the same
thingy than me will be brilliant because we all think that we are brilliant,
but only in the joke.
So there is definitely nothing wrong at all and it is quite impressive for
someone new to ruby.
However it is quite a headache to maintain, I did not add any more readable
forms, using e.g. #inject because I thaught that this will be perfectly at
your fingertips.

Simon has added a version I think is most readable, it is probably a very
good thing to use.

Here is an alternative using #inject - I do not like inject - but I am a
pro, I will not let getting feelings in my way :wink: <---N.B. Smiley

n.inject(){
  >a,k| a << [*k*n+1..(k+1)*n]
}

Furthermore that leaves a little challange for yourself, unless you dislike
the magic dot notation.

Cheers
Robert

···

On 10/17/06, Peter Szinek <peter@rubyrailways.com> wrote:

> On 10/17/06, Peter Szinek <peter@rubyrailways.com> wrote:

Peter
http://www.rubyrailways.com

--
The reasonable man adapts himself to the world; the unreasonable one
persists in trying to adapt the world to himself. Therefore all progress
depends on the unreasonable man.

- George Bernhard Shaw