Hi -- On Sun, 21 Aug 2005, Xeno Campanoli wrote: > Bill Kelly wrote: > >> From: "Xeno Campanoli" <xeno / eskimo.com> >> >>> 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. Let's be fair, though: you can't just airlift what Perl does into Ruby. In Perl you can do things like: $x[2]{"y"}[0] = 1; and have things created automatically because there's no ambiguity. In Ruby, you can't do this without prior initialization: x[2]["y"][0] = 1 because there's no way to tell what the objects should be -- and not just as between arrays and hashes, but at all. They could be anything that responds to '[]'. Even if x is an array, you can't infer what x[2] is supposed to be, except that it is something that can take "y" as an argument to #[]. That could be a hash, a Proc, the class object Array, or arbitrarily many things that you might create in your program. I completely understand your love of how this works in Perl. I just wanted to clarify the fact that it's not an omission or gap in Ruby; it's a consequence of the whole language design. So there are compensations :-) (And see the other responses for ways to achieve something close to what you want.) David -- David A. Black dblack / wobblini.net