From: Robert Klemme Date: 2010-10-26T15:30:29+09:00 Subject: Re: Extraction of single subarrays from multidimensional array On 26.10.2010 01:01, Maurizio Cirilli wrote: > I am a new member of this group and a newbie about Ruby. After a > not successful and > extensive search on this topic, I ask your help solving a problem in > picking out single subarrays > from a multidimesional array. > In short, I somehow stored the following m-array (single strings are > DNA codons): > ss = ["tcg", "agt", "tct", "agc", "tca", "tcc"], ["aaa", "aag"], > ["ctg", "tta", "ctt", "cta", "ctc", "ttg"]] Btw, I assume you do bioinformatics. If all your Arrays contain three letter sequences you should probably change entries to Symbols (there are only 4 ^ 3 = 64 of them). I'd probably replace these Arrays by a custom class for handling sequences and work with that. Then you can optimize internal representation (e.g. use a Fixnum to code a three letter sequence) to save even more memory. I believe there are libraries for bioinformatics out there which probably do exactly that. > What I would like to get are the separated arrays s[0], s[1] and s[2] > by iteration over array ss. > The method array.clone looks perfect for this aim: > > irb(main):039:0> v0 = ss[0].clone > => ["tcg", "agt", "tct", "agc", "tca", "tcc"] > > but I did not find the right way to iterate this method over the m- > array and get indexed subarrays. Do you actually need a copy or do you want to reference the original? If you need the original here's the simplest approach a, b, c = *ss For copy you can do (in 1.9.*) a, b, c = *ss.map &:clone # 1.9.* a, b, c = *ss.map {|x| x.clone} # 1.8.6 and earlier Note that then you still share String instances! So if you want to manipulate individual strings you need to take a different approach (e.g.) a, b, c = *ss.map {|arr| arr.map {|s| s.dup}} a, b, c = *Marshal.load(Marshal.dump(ss)) > I tried iterations like: > > v"#{n}"= ss.clone(n) do |n| > end > or Apart from that it does not work, where's the point in creating variables with calculated names with indexes if you can do indexed access via the Array already? That does not seem like a viable approach. Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/