Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
a deepcopy for "array = [1,2,3]*5"?
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
  4 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
 
Boris  
View profile  
 More options May 23 2005, 8:18 am
Newsgroups: comp.lang.ruby
From: "Boris" <bvsch...@gmail.com>
Date: 23 May 2005 05:18:16 -0700
Local: Mon, May 23 2005 8:18 am
Subject: a deepcopy for "array = [1,2,3]*5"?
Been looking this up in the myriad of deepcopy strands, but couldn't
find it in 30 minutes, so I'm posting the question.

The question is simple. What way would you create an array that
contains 5 copies of an array, without the copies interfering with each
other?

array = [1,2,3]*5

generates:
[[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3]]

but unfortunately also makes 'array[1][1] = "a"' change it like this:

[[1,"a",3],[1,"a",3],[1,"a",3],[1,"a",3],[1,"a",3]]

---------
Anyway, found a solution during tinkering, although I don't know why.
Thought I'd just still post it anyway.

ar = []
5.times{ar << [1,2,3]}

works, and 'array[1][1] = "a"' results in the thing I want

[[1,2,3],[1,"a",3],[1,2,3],[1,2,3],[1,2,3]]


    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.
Brian Schröder  
View profile  
 More options May 23 2005, 8:29 am
Newsgroups: comp.lang.ruby
From: Brian Schröder <ruby.br...@gmail.com>
Date: Mon, 23 May 2005 21:29:01 +0900
Local: Mon, May 23 2005 8:29 am
Subject: Re: a deepcopy for "array = [1,2,3]*5"?
On 23/05/05, Boris <bvsch...@gmail.com> wrote:

For me [1,2,3]*5 behaves different.

irb(main):001:0> [1,2,3]*5
=> [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]

For what you want you could use:

irb(main):002:0> a = Array.new(5) { [1,2,3] }
=> [[1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]
irb(main):003:0> a[1][1] = 'a'
=> "a"
irb(main):004:0> a
=> [[1, 2, 3], [1, "a", 3], [1, 2, 3], [1, 2, 3], [1, 2, 3]]

$ ruby -v
ruby 1.8.2 (2005-04-11) [i386-linux]

Which version of ruby are you using?

best regards,

Brian Schröder

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/


    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.
Florian Frank  
View profile  
 More options May 23 2005, 8:28 am
Newsgroups: comp.lang.ruby
From: "Florian Frank" <fl...@nixe.ping.de>
Date: Mon, 23 May 2005 21:28:08 +0900
Local: Mon, May 23 2005 8:28 am
Subject: Re: a deepcopy for "array = [1,2,3]*5"?
On 2005-05-23 21:20:17 +0900, Boris wrote:

> The question is simple. What way would you create an array that
> contains 5 copies of an array, without the copies interfering with each
> other?

array = Array.new(5) { [1,2,3] }

--
Florian Frank


    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.
Premshree Pillai  
View profile  
 More options May 23 2005, 10:58 am
Newsgroups: comp.lang.ruby
From: Premshree Pillai <premshree.pil...@gmail.com>
Date: Mon, 23 May 2005 23:58:43 +0900
Local: Mon, May 23 2005 10:58 am
Subject: Re: a deepcopy for "array = [1,2,3]*5"?
On 5/23/05, Brian Schröder <ruby.br...@gmail.com> wrote:

> For me [1,2,3]*5 behaves different.

> irb(main):001:0> [1,2,3]*5
> => [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]

He prolly meant [[1, 2, 3]]*5

--
Premshree Pillai


    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