From: Matthew Kerwin Date: 2012-10-02T12:09:35+09:00 Subject: Re: how to read arrary with an array In this case it's pretty simple, if you rearrange the source code you can get a hint at how to read it: chapters = [ [ 'Getting Started', 1 ], [ 'Numbers', 9 ], [ 'Letters', 13 ] ] The 'chapters' variable itself is a flat list of *things*; think of those things as "rows". Each row is *itself* a list, of "columns". So chapters[1][0] can be pronounced "chapters, row 1, column 0", which in this case is the String 'Numbers'. This way of thinking about it, and laying it out, will work as long as there are only two levels of arrays, and the inner arrays are all of the same length. For deeper nestings you'll have to come up with your own way of visualising, but the same idea -- of accessing a single element, and then accessing an element inside it -- still holds. On 2 October 2012 12:19, Richard D. wrote: > Hello. I believe this is basic question, but I'm just starting to learn > programming and choose Ruby to learn from. I grabbed Chris Pine's book > Learn to Program to read, try, and learn from. > > Anyway, the book had a lesson on creating a table contents using arrays. > Below is his less advance solution he gave. For the most part I get it. > The problem, I don't understand how to read the arrary within an array, > or more how the pc reads it. I think that also leads to why I don't > understand the variables being created later as well. > > I tried looking around here, and google, but I'm getting results beyond > the lesson and my understanding. Below is the program and my notes. > > Thanks in advance--Rich. > > # table of contents using arrays > > title = 'Table of Contents' > > chapters = [['Getting Started', 1], ['Numbers', 9], ['Letters', 13]] > # array within an array. > # don't understand how to read > > puts title.center(50) > puts > chap_num = 1 > > chapters.each do |chap| > name = chap[0] # don't undestand chap [0] and chap [1] reference > page = chap[1] # assuming where in master array to pull data > > > beginning = 'Chapter ' + chap_num.to_s + ': ' + name > ending = 'page ' + page.to_s > > puts beginning.ljust(30) + ending.rjust(20) > chap_num = chap_num + 1 > end > > -- > Posted via http://www.ruby-forum.com/. > -- Matthew Kerwin, B.Sc (CompSci) (Hons) http://matthew.kerwin.net.au/ ABN: 59-013-727-651 "You'll never find a programming language that frees you from the burden of clarifying your ideas." - xkcd