tags:

views:

66

answers:

3

I have an array that looks like this.

[{"EntryId"=>"2", "Field1"=>"National Life Group","DateCreated"=>"2010-07-30 11:00:14", "CreatedBy"=>"tristanoneil"},
 {"EntryId"=>"3", "Field1"=>"Barton Golf Club", "DateCreated"=>"2010-07-30 11:11:20", "CreatedBy"=>"public"},
 {"EntryId"=>"4", "Field1"=>"PP&D Brochure Distribution", "DateCreated"=>"2010-07-30 11:11:20", "CreatedBy"=>"public"}, 
 {"EntryId"=>"5", "Field1"=>"Prime Renovation Group, DreamMaker Bath & Kitchen",  "DateCreated"=>"2010-07-30 11:11:21", "CreatedBy"=>"public"}
]

How would I go about iterating through this array so I can specify which field I want to print out and get the value, so I could do something like.

puts EntryId.value
+2  A: 

It looks almost like this is an Array of hashes. Assuming this is stored in a variable like so:

data = [{"EntryId"=>"2", "Field1"=>"National Life Group"},
        {"EntryId"=>"3", "Field1"=>"Barton Golf Club"},
        {"EntryId"=>"4", "Field1"=>"PP&D Brochure Distribution"}
       ]

Single elements of the Array can be accessed with the index in square brackets. Values of hashes can be accessed with the key in square brackets. For example to get the value of "Field1" of the second array element you would use:

data[1]["EntryId"]

You can iterate through the array easily with the methods defined in the Enum mixin.

If you want to process the array you can use each: This code will print the value of the Entry Id for each element in your array.

data.each{|entry| puts entry["EntryId"]}

This data doesn't need to be stored in a variable to work. You could just access the anonymous array directly with these methods:

For example this will return an array of string. Where each element is of the returned array is a formated variant of the corresponding element in the original array.

[{"EntryId"=>"2", "Field1"=>"National Life Group"},
 {"EntryId"=>"3", "Field1"=>"Barton Golf Club"},
 {"EntryId"=>"4", "Field1"=>"PP&D Brochure Distribution"}
].map{|e| "EntryId: #{e["EntryId"]}\t Company Name: #{e["Field1"]}"}
EmFi
+1  A: 

The presence of curly braces and hashrockets (=>) means that you are dealing with a Ruby Hash, not an Array.

Luckily, retrieving the value (the thing to the right of the hashrocket) associated with any one key (the thing to the left of the hashrocket) is a piece of cake with Hashes: all you have to do is use the [] operator.

entry = { "EntryId" => "2", "Field1" => "National Life Group", ... }
entry["EntryId"] # returns "2"

Here is the documentation for Hash: http://ruby-doc.org/core/classes/Hash.html

Raphomet
This worked brilliantly.
Tristan O'Neil
+1  A: 

Whenever I see multi-dimension arrays, I wonder if it can't be made simpler and easier to understand with a small class or struct, which is like a lightweight class.

e.g.

# define the struct
Thing = Struct.new( "Thing", :entry_id, :field_1, :date_created , :created_by)

directory = Hash.new     # create a hash to hold your things keyed by entry_id

# create your things and add them to the hash
thing = Thing.new(2, "National Life Group", "2010-07-30 11:00:14", "tristanoneil" )
directory[thing.entry_id] = thing

thing = Thing.new(3, "Barton Golf Club", "2010-07-30 11:00:14", "public" )
directory[thing.entry_id] = thing                    

thing = Thing.new(4, "PP&D Brochure Distribution", "2010-07-30 11:00:14", "public" )
directory[thing.entry_id] = thing

thing = Thing.new(5, "Prime Renovation Group, DreamMaker Bath & Kitchen", "2010-07-30 11:00:14", "public" )
directory[thing.entry_id] = thing


# then retrieve what you want from the hash
my_thing = directory[3]
puts my_thing.field_1

The advantage of creating a struct like that to hold your data is that you can do whatever you want with each item - put them into arrays, hashes, whatever, and still access each individual item and its fields by the object.fieldname notation.

stephenr