Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Array.new question
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  9 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Peter Szinek  
View profile  
 More options Oct 17 2006, 4:59 am
Newsgroups: comp.lang.ruby
From: Peter Szinek <pe...@rubyrailways.com>
Date: Tue, 17 Oct 2006 17:59:06 +0900
Local: Tues, Oct 17 2006 4:59 am
Subject: 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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alex Young  
View profile  
 More options Oct 17 2006, 5:08 am
Newsgroups: comp.lang.ruby
From: Alex Young <a...@blackkettle.org>
Date: Tue, 17 Oct 2006 18:08:08 +0900
Local: Tues, Oct 17 2006 5:08 am
Subject: Re: Array.new question
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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Szinek  
View profile  
 More options Oct 17 2006, 5:20 am
Newsgroups: comp.lang.ruby
From: Peter Szinek <pe...@rubyrailways.com>
Date: Tue, 17 Oct 2006 18:20:45 +0900
Local: Tues, Oct 17 2006 5:20 am
Subject: Re: Array.new question

> 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 ;)

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alex Young  
View profile  
 More options Oct 17 2006, 5:45 am
Newsgroups: comp.lang.ruby
From: Alex Young <a...@blackkettle.org>
Date: Tue, 17 Oct 2006 18:45:23 +0900
Local: Tues, Oct 17 2006 5:45 am
Subject: Re: Array.new question

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ross Bamford  
View profile  
 More options Oct 17 2006, 5:49 am
Newsgroups: comp.lang.ruby
From: Ross Bamford <ros...@roscopeco.co.uk>
Date: Tue, 17 Oct 2006 18:49:10 +0900
Local: Tues, Oct 17 2006 5:49 am
Subject: Re: Array.new question

I think this will do the same thing, too:

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

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

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

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
eden li  
View profile  
 More options Oct 17 2006, 5:51 am
Newsgroups: comp.lang.ruby
From: "eden li" <eden...@gmail.com>
Date: Tue, 17 Oct 2006 18:51:30 +0900
Local: Tues, Oct 17 2006 5:51 am
Subject: Re: Array.new question

> 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 ;)

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Szinek  
View profile  
 More options Oct 17 2006, 5:52 am
Newsgroups: comp.lang.ruby
From: Peter Szinek <pe...@rubyrailways.com>
Date: Tue, 17 Oct 2006 18:52:01 +0900
Local: Tues, Oct 17 2006 5:52 am
Subject: Re: Array.new question
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 :-) as someone proposed on this list ;-)

Peter
http://www.rubyrailways.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Peter Szinek  
View profile  
 More options Oct 17 2006, 6:16 am
Newsgroups: comp.lang.ruby
From: Peter Szinek <pe...@rubyrailways.com>
Date: Tue, 17 Oct 2006 19:16:04 +0900
Local: Tues, Oct 17 2006 6:16 am
Subject: Re: Array.new question

Robert Dober wrote:
> On 10/17/06, Peter Szinek <pe...@rubyrailways.com> wrote:
> You are simply brilliant ;)

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Simon Kröger  
View profile  
 More options Oct 17 2006, 6:39 am
Newsgroups: comp.lang.ruby
From: Simon Kröger <SimonKroe...@gmx.de>
Date: Tue, 17 Oct 2006 19:39:25 +0900
Local: Tues, Oct 17 2006 6:39 am
Subject: Re: Array.new question
Peter Szinek schrieb:

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

>> You are simply brilliant ;)

> 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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google