Referring to Hash and Array Elements

S

Saeed Bhuta

Hi All,

I am a beginner to Ruby. Currently I am working with Hashes and Arrays.

I have a loop where the code saves several objects into a Hash.
Everytime the loop is run the objects in the Hash are overwritten,
therefore I append the objects into an Array so that data is not lost.

#begining of loop ommitted

trainHash = {}
trainHash['order'] = tubeOrder
trainHash['destination'] = destination
trainHash['currentLocation'] = currentLocation
trainHash['dueIn'] = dueInMinsValue
#trainHash['dueintimestamp'] = dueInTimestamp.to_s

trainArray << trainHash
end

From what I have read so far, Arrays are indexed by an integer with
indexing starting at '0'.

So for example, if the above loop was run twice; the number of objects
in the trainArray would be two and referred using [0] for the first
object and [1] for the second object.

My question is, is it possible to refer the elements as they were stored
in the Hash? For example, I want the code to return the value of the
'order' field in the second object in the trainArray.

If not, is it possible to dynamically generate a key for the hash
everytime the loop is run so that the objects are not overwritten in the
hash?

I would sincerely appreciate any help.

Saeed.
 
R

Robert Klemme

2010/2/24 Saeed Bhuta said:
Hi All,

I am a beginner to Ruby. Currently I am working with Hashes and Arrays.

I have a loop where the code saves several objects into a Hash.
Everytime the loop is run the objects in the Hash are overwritten,
therefore I append the objects into an Array so that data is not lost.

#begining of loop ommitted

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0trainHash =3D {}
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0trainHash['order'] =3D tubeOrder
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0trainHash['destination'] =3D destination
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0trainHash['currentLocation'] =3D currentLo= cation
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0trainHash['dueIn'] =3D dueInMinsValue
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0#trainHash['dueintimestamp'] =3D dueInTime= stamp.to_s

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0trainArray << trainHash
=A0 =A0 =A0 =A0 =A0 =A0 end

From what I have read so far, Arrays are indexed by an integer with
indexing starting at '0'.

So for example, if the above loop was run twice; the number of objects
in the trainArray would be two and referred using [0] for the first
object and [1] for the second object.

My question is, is it possible to refer the elements as they were stored
in the Hash?

I am not sure I understand that question.
For example, I want the code to return the value of the
'order' field in the second object in the trainArray.

Like this?

second_order =3D train_array[1]['order']
If not, is it possible to dynamically generate a key for the hash
everytime the loop is run so that the objects are not overwritten in the
hash?

Yes, of course. There are also other options. It depends how you
want to further process the data. To me it looks like all values in
the Hash belong to a single record. In this case I would stick with
the two level approach, i.e. have an Array of records (Hashes or other
types).

If you just want to collect all individual values per key, you can use
Arrays as Hash values, e.g.

train_hash =3D Hash.new {|h,k| h[k] =3D []}

# loop
train_hash['order'] << tube_order
train_hash['destination'] << destination
train_hash['currentLocation'] << current_location
train_hash['dueIn'] << due_in_mins_value
#train_hash['dueintimestamp'] << due_in_timestamp.to_s

# end loop

Btw, I changed names to match the usual convention in Ruby.

Kind regards

robert


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
R

Rick DeNatale

Hi All,

I am a beginner to Ruby. Currently I am working with Hashes and Arrays.

I have a loop where the code saves several objects into a Hash.
Everytime the loop is run the objects in the Hash are overwritten,
therefore I append the objects into an Array so that data is not lost.

#begining of loop ommitted

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0trainHash =3D {}
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0trainHash['order'] =3D tubeOrder
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0trainHash['destination'] =3D destination
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0trainHash['currentLocation'] =3D currentLo= cation
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0trainHash['dueIn'] =3D dueInMinsValue
=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0#trainHash['dueintimestamp'] =3D dueInTime= stamp.to_s

=A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0trainArray << trainHash
=A0 =A0 =A0 =A0 =A0 =A0 end

From what I have read so far, Arrays are indexed by an integer with
indexing starting at '0'.

So for example, if the above loop was run twice; the number of objects
in the trainArray would be two and referred using [0] for the first
object and [1] for the second object.

My question is, is it possible to refer the elements as they were stored
in the Hash? For example, I want the code to return the value of the
'order' field in the second object in the trainArray.

Robert already answered this question. I'd like to point out that
your code for building the hashes inserted into the array could be
simplified:

#begining of loop ommitted

trainArray << { 'order' =3D> tubeOrder, 'destination' =3D>
destination, 'currentLocation' =3D> currentLocation,
'dueIn' =3D> dueInMinsValue,
'dueintimestamp' =3D> dueInTimestamp.to_s}

end

--=20
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top