Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > how to copy array in ruby

Reply
Thread Tools

how to copy array in ruby

 
 
PIKI
Guest
Posts: n/a
 
      11-13-2009
HI

I am a novice in Ruby programming so this question might look trivial
to lot of people.

But I am struggling to copy an array.

I am using

a = []
b = a

/* some manipulation here on "b" and "a" I havent touched at all*/

Here when i am trying to see contents of "a" and "b" both have same
contents

But my expectation was they shud be different

Instead of b = a, I also tried b.replace(a)

but no luck here also.

Please help me on how to make a duplicate("b") of "a" so that changes
done on b will not affect a

Thanks
Pikender Sharma
 
Reply With Quote
 
 
 
 
Jesús Gabriel y Galán
Guest
Posts: n/a
 
      11-13-2009
On Fri, Nov 13, 2009 at 2:30 PM, PIKI <> wrote:

> But I am struggling to copy an array.
>
> I am using
>
> a = []
> b = a
>
> /* some manipulation here on "b" and "a" I havent touched at all*/
>
> Here when i am trying to see contents of "a" and "b" both have same
> contents
>
> But my expectation was they shud be different
>
> Instead of b = a, I also tried b.replace(a)
>
> but no luck here also.
>
> Please help me on how to make a duplicate("b") of "a" so that changes
> done on b will not affect a


irb(main):001:0> a = [1,2,3,4]
=> [1, 2, 3, 4]
irb(main):002:0> b = a.dup
=> [1, 2, 3, 4]
irb(main):003:0> b[0] = 55
=> 55
irb(main):004:0> a
=> [1, 2, 3, 4]
irb(main):005:0> b
=> [55, 2, 3, 4]

Hope this helps,

Jesus.

 
Reply With Quote
 
 
 
 
PIKI
Guest
Posts: n/a
 
      11-14-2009
On Nov 13, 6:34*pm, Jesús Gabriel y Galán <jgabrielyga...@gmail.com>
wrote:
> On Fri, Nov 13, 2009 at 2:30 PM, PIKI <avid.p...@gmail.com> wrote:
> > But I am struggling tocopyanarray.

>
> > I am using

>
> > a = []
> > b = a

>
> > /* some manipulation here on "b" and "a" I havent touched at all*/

>
> > Here when i am trying to see contents of "a" and "b" both have same
> > contents

>
> > But my expectation was they shud be different

>
> > Instead of b = a, I also tried b.replace(a)

>
> > but no luck here also.

>
> > Please help me on how to make a duplicate("b") of "a" so that changes
> > done on b will not affect a

>
> irb(main):001:0> a = [1,2,3,4]
> => [1, 2, 3, 4]
> irb(main):002:0> b = a.dup
> => [1, 2, 3, 4]
> irb(main):003:0> b[0] = 55
> => 55
> irb(main):004:0> a
> => [1, 2, 3, 4]
> irb(main):005:0> b
> => [55, 2, 3, 4]
>
> Hope this helps,
>
> Jesus.- Hide quoted text -
>
> - Show quoted text -


Hi Jesús Gabriel y Galán

Thanks for the reply and help

I tried ur solution but it is also not working

I am unable to figure out the trick

Thanks
Pikender Sharma
 
Reply With Quote
 
lith
Guest
Posts: n/a
 
      11-14-2009
> I tried ur solution but it is also not working
>
> I am unable to figure out the trick


In order to help us to figure out what is the problem, you might want
to post a code snippet demonstrating the defect and explain what how
you would expect ruby to behave.

Array#dup creates a copy of the array, not of the elements in the
array, aka a shallow copy.

 
Reply With Quote
 
Ralph Shnelvar
Guest
Posts: n/a
 
      11-14-2009
l> Array#dup creates a copy of the array, not of the elements in the
l> array, aka a shallow copy.

Is there a way to make a deep copy? That is ... in the code below,
could b.dup be replaced with something like b.deepdup?


irb(main):019:0> a1 = [1,2,3,4]
=> [1, 2, 3, 4]
irb(main):020:0> b = [a1,5]
=> [[1, 2, 3, 4], 5]
irb(main):021:0> c1 = b
=> [[1, 2, 3, 4], 5]
irb(main):022:0> c2 = b.dup
=> [[1, 2, 3, 4], 5]
irb(main):023:0> a1[0] = "hi"
=> "hi"
irb(main):024:0> a1
=> ["hi", 2, 3, 4]
irb(main):025:0> c1
=> [["hi", 2, 3, 4], 5]
irb(main):026:0> c2
# because of the shallow copy, c2[0][0] is "hi" instead of a naively
# assumed 1.
=> [["hi", 2, 3, 4], 5]
irb(main):027:0> c1[1] = "bye"
=> "bye"
irb(main):028:0> c1
=> [["hi", 2, 3, 4], "bye"]
irb(main):029:0> c2
# The shallow copy gives the expected result for c2[1], below.
=> [["hi", 2, 3, 4], 5]



 
Reply With Quote
 
Derrick Fiedler
Guest
Posts: n/a
 
      11-14-2009
you can make it via this method "clone" b =3D a.clone
Am 14.11.2009 um 11:25 schrieb PIKI:

> On Nov 13, 6:34 pm, Jes=FAs Gabriel y Gal=E1n =

<jgabrielyga...@gmail.com>
> wrote:
>> On Fri, Nov 13, 2009 at 2:30 PM, PIKI <avid.p...@gmail.com> wrote:
>>> But I am struggling tocopyanarray.

>>
>>> I am using

>>
>>> a =3D []
>>> b =3D a

>>
>>> /* some manipulation here on "b" and "a" I havent touched at all*/

>>
>>> Here when i am trying to see contents of "a" and "b" both have same
>>> contents

>>
>>> But my expectation was they shud be different

>>
>>> Instead of b =3D a, I also tried b.replace(a)

>>
>>> but no luck here also.

>>
>>> Please help me on how to make a duplicate("b") of "a" so that =20
>>> changes
>>> done on b will not affect a

>>
>> irb(main):001:0> a =3D [1,2,3,4]
>> =3D> [1, 2, 3, 4]
>> irb(main):002:0> b =3D a.dup
>> =3D> [1, 2, 3, 4]
>> irb(main):003:0> b[0] =3D 55
>> =3D> 55
>> irb(main):004:0> a
>> =3D> [1, 2, 3, 4]
>> irb(main):005:0> b
>> =3D> [55, 2, 3, 4]
>>
>> Hope this helps,
>>
>> Jesus.- Hide quoted text -
>>
>> - Show quoted text -

>
> Hi Jes=FAs Gabriel y Gal=E1n
>
> Thanks for the reply and help
>
> I tried ur solution but it is also not working
>
> I am unable to figure out the trick
>
> Thanks
> Pikender Sharma
>



 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
what is Deep Copy, shallow copy and bitwises copy.? saxenavaibhav17@gmail.com C++ 26 09-01-2006 09:37 PM
is dict.copy() a deep copy or a shallow copy Alex Python 2 09-05-2005 07:01 AM
[help] How to copy fast an array to another array? stelladiary2004@hotmail.com C++ 4 08-13-2004 01:48 AM
How do copy Strings from a single dimensional array to double dimensional array Venkat C++ 4 12-05-2003 09:23 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57