Hm,... arr[1]["name"]

$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

How about:

arr =
(0…9).each { |i|
arr[i] = {}
arr[i][“name”] = daniel
arr[i][“age”] = 20
}

Jeff.

···

On Thu, Feb 13, 2003 at 05:45:18AM +0900, daniel wrote:

$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


Jeff Putsch Email: putsch@mxim.com
Maxim Integrated Products Office: (503)547-2037
High Frequency CAD Engineering

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.

···

On Thu, Feb 13, 2003 at 05:45:18AM +0900, daniel wrote:

$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?


Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137

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

daniel wrote:

$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
}

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)

Hi –

···

On Thu, 13 Feb 2003, daniel wrote:

$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
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

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

···

daniel offstuff@aon.at wrote:

$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’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

···

On Wed, 12 Feb 2003, daniel wrote:

$arr = Array();
for( $i=0; $i<10; $i++ )
{
$arr[$i][“name”] = “daniel”;
$arr[$i][“age”] = 20;
}

====================================

Ara Howard
NOAA Forecast Systems Laboratory
Information and Technology Services
Data Systems Group
R/FST 325 Broadway
Boulder, CO 80305-3328
Email: ahoward@fsl.noaa.gov
Phone: 303-497-7238
Fax: 303-497-7259
====================================

“daniel” offstuff@aon.at schrieb im Newsbeitrag
news:3e4ab104$0$33128$91cee783@newsreader02.highway.telekom.at…

$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

dblack@candle.superlink.net wrote:

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? :slight_smile:

martin

shit,

it’s easy g

I’v thought too complex

thx to all,
daniel

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.

···

On Thu, Feb 13, 2003 at 06:02:50AM +0900, Chris Pine wrote:

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

Hi,

···

In message “Re: hm,… arr[1][“name”]” on 03/02/13, “Robert Klemme” bob.news@gmx.net writes:

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.

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.

arr =
10.times { arr << {} }

(0…9).map { {} }

···

On Thu, Feb 13, 2003 at 08:28:58AM +0900, Brian Candler wrote:

On Thu, Feb 13, 2003 at 06:02:50AM +0900, Chris Pine wrote:


_ _

__ __ | | ___ _ __ ___ __ _ _ __
'_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
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

“Yukihiro Matsumoto” matz@ruby-lang.org schrieb im Newsbeitrag
news:1045126395.130996.11025.nullmailer@picachu.netlab.jp…

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

Hi,

···

In message “Re: hm,… arr[1][“name”]” on 03/02/13, “Robert Klemme” bob.news@gmx.net writes:

a = (1…10).collect{{“name” => “daniel”, “age” => 20}}

Darn! That was the solution I was looking for. Thanks a lot!

If you’re using 1.8, it’s even simpler:

a = Array.new(10){{“name” => “daniel”, “age” => 20}}

						matz.