Author |
Message |
danie #1 / 17
|
 hm,... arr[1]["name"]
$arr = Array(); for( $i=0; $i<10; $i++ ) { $arr[$i]["name"] = "daniel"; $arr[$i]["age"] = 20; Quote: }
This was php. How to do this in ruby? Until yet the best way I found was to create two classes... I come from c/c++ and php - so I don't know how perl manage such things. In the ruby documentations arrays would not expatiated. daniel
|
Mon, 01 Aug 2005 04:41:21 GMT |
|
 |
Kent Dah #2 / 17
|
 hm,... arr[1]["name"]
Quote:
> $arr = Array(); > for( $i=0; $i<10; $i++ ) > { > $arr[$i]["name"] = "daniel"; > $arr[$i]["age"] = 20; > } > This was php. > How to do this in ruby?
I'd use a Hash for each of the entries in the Array: arr = Array.new 10.times{|i| arr[i]={} # or Hash.new arr[i]["name"] = "daniel" arr[i]["age"] = 20 Quote: }
Not much of a PHP man, so I'm probably a bit of the mark. -- (\[ Kent Dahl ]/)_ _~_ __[ http://www.stud.ntnu.no/~kentda/ ]___/~ ))\_student_/(( \__d L b__/ NTNU - graduate engineering - 5. year ) ( \__\_?|?_/__/ ) _)Industrial economics and technological management( \____/_?_\____/ (____engineering.discipline_=_Computer::Technology___)
|
Mon, 01 Aug 2005 04:47:52 GMT |
|
 |
Daniel Carrer #3 / 17
|
 hm,... arr[1]["name"]
Quote:
> $arr = Array(); > for( $i=0; $i<10; $i++ ) > { > $arr[$i]["name"] = "daniel"; > $arr[$i]["age"] = 20; > } > This was php. > How to do this in ruby?
arr = Array.new (0..9).each do |i| arr[i] = Hash.new arr[i]["name"] = "daniel" arr[i]["age"] = 20 end You could replace "Array.new" and "Hash.new" by "[]" and "{}" respectively if you prefer. -- Daniel Carrera Graduate Teaching Assistant. Math Dept. University of Maryland. (301) 405-5137
|
Mon, 01 Aug 2005 04:50:49 GMT |
|
 |
Jeff Putsc #4 / 17
|
 hm,... arr[1]["name"]
How about: arr = [] (0..9).each { |i| arr[i] = {} arr[i]["name"] = daniel arr[i]["age"] = 20 } Jeff. Quote:
> $arr = Array(); > for( $i=0; $i<10; $i++ ) > { > $arr[$i]["name"] = "daniel"; > $arr[$i]["age"] = 20; > } > This was php. > How to do this in ruby? > Until yet the best way I found was to create two classes... > I come from c/c++ and php - so I don't know how perl > manage such things. In the ruby documentations arrays would > not expatiated. > daniel
--
Maxim Integrated Products Office: (503)547-2037 High Frequency CAD Engineering
|
Mon, 01 Aug 2005 04:50:40 GMT |
|
 |
Chris Pin #5 / 17
|
 hm,... arr[1]["name"]
Another way: # Create an empty hash in each slot of the array. arr = Array.new (10) { {} } # same as: # arr = Array.new (10) do # Hash.new # end arr.each do |personHash| personHash[:name] = 'daniel' personHash[:age] = 20 end And another way: arr = [] 10.times do arr << {:name => 'daniel', :age => 20} end Chris
|
Mon, 01 Aug 2005 05:02:50 GMT |
|
 |
Martin DeMell #6 / 17
|
 hm,... arr[1]["name"]
Quote:
> $arr = Array(); > for( $i=0; $i<10; $i++ ) > { > $arr[$i]["name"] = "daniel"; > $arr[$i]["age"] = 20; > } > This was php. > How to do this in ruby?
arr = (0...10).map { {"name" => "daniel", "age" => 20} } or, more explicitly and in two steps arr = (0...10).map {Hash.new} arr.each {|i| i["name"] = "daniel"; i["age"] = 20} martin
|
Mon, 01 Aug 2005 05:32:18 GMT |
|
 |
dbl.. #7 / 17
|
 hm,... arr[1]["name"]
Hi -- Quote:
> $arr = Array(); > for( $i=0; $i<10; $i++ ) > { > $arr[$i]["name"] = "daniel"; > $arr[$i]["age"] = 20; > } > This was php. > How to do this in ruby?
Here's another idiom to add to the collection: arr = (0..9).map {|x| { "name" => "daniel", "age" => 20 } } (where the inner {} are the literal hash constructor) David -- David Alan Black
Web: http://pirate.shu.edu/~blackdav
|
Mon, 01 Aug 2005 05:42:31 GMT |
|
 |
Martin DeMell #8 / 17
|
 hm,... arr[1]["name"]
Quote:
> Here's another idiom to add to the collection: > arr = (0..9).map {|x| { "name" => "daniel", "age" => 20 } } > (where the inner {} are the literal hash constructor)
Do I hear a vote for 10.map == (0...10).map? :) martin
|
Mon, 01 Aug 2005 05:49:36 GMT |
|
 |
danie #9 / 17
|
 hm,... arr[1]["name"]
shit, it's easy *g* I'v thought too complex thx to all, daniel
|
Mon, 01 Aug 2005 05:51:37 GMT |
|
 |
ahowar #10 / 17
|
 hm,... arr[1]["name"]
Quote:
> $arr = Array(); > for( $i=0; $i<10; $i++ ) > { > $arr[$i]["name"] = "daniel"; > $arr[$i]["age"] = 20; > }
i'm sure everyone will suggest a hash, but i use a little module i wrote which will allows array's to have named fields, which is nice because then all the normal array operators work, usage is as follows : require 'arrayfields' fields = [ 'name', 'age' ] table = [] 10.times do (row = []).fields = fields row['name'] = 'daniel' row['age'] = 20 table << row end table.map {|row| p row} ~/eg/ruby > ruby foo.rb ["daniel", 20] ["daniel", 20] ["daniel", 20] ["daniel", 20] ["daniel", 20] ["daniel", 20] ["daniel", 20] ["daniel", 20] ["daniel", 20] ["daniel", 20] http://eli.fsl.noaa.gov/lib/ruby/site_ruby/arrayfields.rb -a -- ==================================== | Ara Howard | NOAA Forecast Systems Laboratory | Information and Technology Services | Data Systems Group | R/FST 325 Broadway | Boulder, CO 80305-3328
| Phone: 303-497-7238 | Fax: 303-497-7259 ====================================
|
Mon, 01 Aug 2005 06:22:40 GMT |
|
 |
Brian Candle #11 / 17
|
 hm,... arr[1]["name"]
Quote:
> Another way: > # Create an empty hash in each slot of the array. > arr = Array.new (10) { {} } > # same as: > # arr = Array.new (10) do > # Hash.new > # end
That doesn't work under ruby-1.6.8: arr = Array.new(10) { {} } => [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil] Some care is required. You can't do: arr = Array.new(10, {} ) => [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}] because then you have got ten references to the same hash: arr.collect{|i| i.id}.join(',') => "67787102,67787102,67787102,67787102,67787102,67787102,67787102,67787102,67787102,67787102" f You need something like: arr = [] 10.times { arr << {} } Regards, Brian.
|
Mon, 01 Aug 2005 07:28:58 GMT |
|
 |
Robert Klemm #12 / 17
|
 hm,... arr[1]["name"]
Quote: > $arr = Array(); > for( $i=0; $i<10; $i++ ) > { > $arr[$i]["name"] = "daniel"; > $arr[$i]["age"] = 20; > } > This was php. > How to do this in ruby?
a = [] 10.times do a.push( { "name" => "daniel", "age" => 20 } ) end You can squeeze it into two lines, if you like: a = [] 10.times { a.push( { "name" => "daniel", "age" => 20 } ) } Regards robert
|
Mon, 01 Aug 2005 16:46:00 GMT |
|
 |
Yukihiro Matsumo #13 / 17
|
 hm,... arr[1]["name"]
Hi, In message "Re: hm,... arr[1]["name"]"
|> How to do this in ruby? | |a = [] |10.times do | a.push( { "name" => "daniel", "age" => 20 } ) |end | |You can squeeze it into two lines, if you like: | |a = [] |10.times { a.push( { "name" => "daniel", "age" => 20 } ) } Even in a line: a = (1..10).collect{{"name" => "daniel", "age" => 20}} matz.
|
Mon, 01 Aug 2005 17:15:45 GMT |
|
 |
Mauricio Fernánde #14 / 17
|
 hm,... arr[1]["name"]
Quote:
> > Another way: > > # Create an empty hash in each slot of the array. > > arr = Array.new (10) { {} } > > # same as: > > # arr = Array.new (10) do > > # Hash.new > > # end > That doesn't work under ruby-1.6.8: > arr = Array.new(10) { {} } > => [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]
In 1.6.x the block is ignored. The change was introduced in 1.7. Quote: > arr = [] > 10.times { arr << {} }
(0..9).map { {} } -- _ _ | |__ __ _| |_ ___ _ __ ___ __ _ _ __ | '_ \ / _` | __/ __| '_ ` _ \ / _` | '_ \ | |_) | (_| | |_\__ \ | | | | | (_| | | | | |_.__/ \__,_|\__|___/_| |_| |_|\__,_|_| |_| Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com The easiest way to get the root password is to become system admin. -- Unknown source
|
Mon, 01 Aug 2005 17:17:03 GMT |
|
 |
Robert Klemm #15 / 17
|
 hm,... arr[1]["name"]
Quote: > Even in a line: > a = (1..10).collect{{"name" => "daniel", "age" => 20}}
Darn! That was the solution I was looking for. Thanks a lot! It's always amazing, how many ways there are to do a job. And I learn something new every day. robert
|
Mon, 01 Aug 2005 18:21:12 GMT |
|
|
Page 1 of 2
|
[ 17 post ] |
|
Go to page:
[1]
[2] |
|