From: lionbarrage Date: 2009-02-26T16:14:17+09:00 Subject: Re: Help with Multi Dimensional Array On Feb 24, 8:58 pm, Simon Krahnke wrote: > * lionbarrage (02:17) schrieb: > > > > > File 1 > > ALPHA|OMEGA|GAMMA > >           1 |         2 |           3 > >           4 |         5 |           6 > > > File 2 > > EPSILON|GREEK|OMEGA|BETA > >           7 |         8 |           9 |        0 > >          12 |           |          13 | > >          10 |        11 |           5 |       15 > > > End result should be: > > > ALPHA|OMEGA|GAMMA|EPSILON|GREEK|BETA > >           1 |         2 |           3 |          |          | > >           4 |         5 |           6 |       10 |       11 |     15 > >             |         9 |             |        7 |        8 |      0 > >             |        13 |             |       12 |          | > > I hoped I fixed your ASCII art the right way. > > In understand that this represents named sets of integers that are to be > merged. Right? > > The natural representation of that would be a hash containing sets. > There is a set class, but I use arrays here: > > require 'pp' > > sets = Hash.new { | h, k | h[k] = [] } # hash that contains a newarray >                                        # for every new key > %w(file1.txt file2).each do | filename | >   File.open(filename) do | f | >     names = f.gets.chop.split('|') >     f.each do | line | >        names.zip(line.chop.split('|')).each do | name, value | >          sets[name] << value >        end >     end >   end > end > > pp sets > > This code does no tabular printing, treats the "integers" as strings, > without excluding empty strings and doubles. But that could easily be > fixed. > > Probably I got it all wrong. :-( > > mfg,                        simon .... l Thanks for fixing my art, looked fine in my screen but I guess the translation was off. I'm not too familiar with hashes but the code you wrote seems to be doing it. Thank you so much!! How do i refer to a single element within a hash? Let's say I want 11 for example. Would I do sets["GREEK"][3]? THANK YOU!