From: Xeno Campanoli Date: 2005-08-21T12:34:08+09:00 Subject: Re: Ruby 2 Idea: You've got to have a[a][b][c][d][e][f][g][h][i]... --------------080209010304040603030602 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Bill Kelly wrote: >From: "Xeno Campanoli" > > >>There are just too many problems that can quickly be solved by >>multi-dimensional arrays and hashes not to have them and have them >>easily. I used the hack from "matz" today to make a 2d hash, and it's >>ugly and unworthy of Ruby. You should be able to do this without any >>extra steps just like in Perl. If the project ends up getting bigger >>THEN you refactor it, but YOU JUST GOT TO HAVE THAT. >> >> > >I don't know how to do it with Array, because Array doesn't >seem to accept a block for its default value generation like >Hash does. > >But with Hash: > >hoh = lambda { Hash.new {|h,k| h[k] = hoh.call} } > > Thank you. I used this just yesterday, and it helped a lot with a report reorganization I wanted to do. The first time I used Perl for a web report (Perl IV I think) I believe it didn't have multi-dimensional hashes either. Once I got Perl5, I could do a lot more much more easily and still clearly. I agree with what the one fellow said that I don't use multi-dimensional arrays nearly as much as multi-dimensional hashes, but for instance it would be nice to have as a memory item on a multi-dimensional hash read. I know in the long run it's better to use more formal constructs for a lot of things in order to make a program clear and maintainable, but really n>2 arrays and hashes are really the thing I love most about Perl, and I think it allows you to do organizations that aid understanding in very important ways with reports that can be written ad hoc very quickly. These are kinds of things that, from my experience, really leverage strong management of things, but are over the head of many people in the world (but presumably never a Ruby programmer). Most people don't make busy, thick reports because the object is to impress the boss, not to leverage your best users, but I'm lucky at getting work situations where I can do the latter and not worry as much about the former. Uh oh, I'm bragging. Avert! I'm tempted to post some of yesterdays code, but it's for Real Networks, and it's probably more appropriate not to pass it around. Oh, I guess that means I'm doing a real project with Ruby by the way. Thanks for the help. Go Ruby! > > >>>x = hoh.call # create autovivifying hash-of-hashes >>> >>> >=> {} > > >>>x[1][2][3][4][5] = "spleen" >>> >>> >=> "spleen" > > >>>x >>> >>> >=> {1=>{2=>{3=>{4=>{5=>"spleen"}}}}} > > >Regards, > >Bill > > > > > > -- Xeno Campanoli, xeno@eskimo.com, http://www.eskimo.com/~xeno The Internet: I'd rather have Al invent it than have George take it over. --------------080209010304040603030602--