From: Jan Reitz Date: 2006-10-11T22:20:09+09:00 Subject: Re: PHP-like Array/Hash creation in Ruby single dimension arrays are simple, because i just need that "a = []", thats true and not much additional work. and they grow for themselves too even if i initialize them with eg. Array.new(16). all great. but the autogrowing is limited to the last dimension. in a[x][y] the y will grow when i assign something to them, the x too but it would contain nil instead if "Array", thats because the a[x] dimension is a "Array of Objects" and not an "Array of Arrays", the a[x] does not know that autoresizes should spawn new "Array"s in the newly generated spaces. i dont have constant size of elements that could be added in just one constant line, i have data comming in from DB's, files and network. take 3-dimensional (points in space) data that i read from a file and i want to store them in a 3 dimensional array. without autogrowing i have to read the file completely and get the mins and maxs and have an array created with this dimensions, then i read the file again (ok i could have stored it, but i have to go over it again) and put the contents in the array OR file.each { |x,y,z,data| a[x] = [] if a[x].nil ? a[x][y] = [] if a[x][y].nil ? a[x][y][z] = data } OR file.each { |x,y,z,data| a[x][y][z] = data } method 2 might look ok, but why not have it easier with method 3? the a = [] is a shortcut. the h = {} too. why not have another shortcut if its technically possible ? you could even determine if the key is not a fixnum and create a Hash instead in php the Arrays are Hashes too (i dont know if all the Arrays are Hashes). we (at my company) sometimes use them like xml, meaning no common size (how great can x be) and depth ([x][y][z][n...]) among the elements. machines['VP21']['status'] = 'running' machines['VP21']['rotation_speed'] = 210 machines['VP21']['workers'] = ['Peter','Paul','Mary'] or better machines['VP21'][:status] = :running machines['VP21'][:rotation_speed] = 210 machines['VP21'][:workers] = ['Peter','Paul','Mary'] and no, i dont need this to be a class, that would double/tripple my work, because 'VP22' has nothing that rotates.... Jules wrote: > Could you post some real php code that uses this feature? It isn't a > problem if you code it right. Instead of: > > a = nil > a[12] = "something" > > You can just do: > > a = [] > a[12] = "something" > > and instead of: > > a = nil > a[12][0] = "foo" > a[12][1] = "bar" > a[12][2] = "baz" > > You use: > > a = [] > a[12] = ["foo", "bar", "baz"] > > Jan Reitz wrote: > > I used PHP for a long time, a little bit C# and recently started to > > learn about Ruby. > > I do like Ruby very much from the first looks > > It has "my_array.sort" like in c#, not that ugly (imo) "sort($myarray)" > > And it has q'n'd qualitys like php for example: definition at first > > assignment, no need for interfaces and so on. > > > > But! one thing i really miss from PHP just writing : > > $my_array[4][5] = "moep"; (no initialize before) > > this does not work in ruby, i had to > > my_array = Array.new(16) { Array.new 16 } > > first. > > and if the array wasn't great enough i end up with element 16 (the > > 17th) complaining: > > NoMethodError: undefined method `[]=' for nil:NilClass > > (this only occurs at "my_array[16][5] = 1", > > not at "my_array[4][16] = 1") > > > > hm, so i thought, if NilClass has no []= method, why dont i define it, > > (i read very often that this is rubys great adv. over most languages > > out there, that u can extend even the basic classes), so it becomes an > > array in that place... but that included "self = Array.new" which > > caused another complaint: "Can't change the value of self" > > > > I read alot of topics about cant change value of self, and i think that > > i have a different problem than these discussed before. > > i dont want to 1.next! => 2 so please dont tell me that i can't do > > that, i know that, and i know its junk > > i want just: > > hans = nil (or even omit this line) > > hans[1] = "test" > > or > > my_array = Array.new(16) { Array.new 16 } > > my_array[16][1] = 1 > > not: > > nil[1] = "test" > > > > I hope my English was well enough to be understood. > > > > - Jan Reitz