Forum: Ruby parsing List results

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B46cf7a3b580f672bfb8b8e2aed6d6f1?d=identicon&s=25 Fernando Ramos (ramosbrazil)
on 2009-01-19 18:01
Hi Folks,

I need to parse a result that comes in this format :

=========================
 ec2.describe_instances #=>
   [{:aws_image_id       => "ami-e444444d",
     :aws_reason         => "",
     :aws_state_code     => "16",
     :aws_owner          => "000000000888",
     :aws_instance_id    => "i-123f1234",
     :aws_reservation_id => "r-aabbccdd",
     :aws_state          => "running",
     :dns_name           =>
"domU-12-34-67-89-01-C9.usma2.compute.amazonaws.com",
     :ssh_key_name       => "staging",
     :aws_groups         => ["default"],
     :private_dns_name   =>
"domU-12-34-67-89-01-C9.usma2.compute.amazonaws.com",
     :aws_instance_type  => "m1.small",
     :aws_launch_time    => "2008-1-1T00:00:00.000Z"},
     :aws_availability_zone => "us-east-1b",
     :aws_kernel_id      => "aki-ba3adfd3",
     :aws_ramdisk_id     => "ari-badbad00",
      ..., {...}]
=========================

When I invoke the method,

puts @ec2.describe_instances

the results comes like this :
=========================
aws_image_idami-e444444daws_reasonaws_state_codeaws_owner000000000888aws_instance_id
i-123f1234aws_reservation_idr-aabbccddaws_staterunningdns_name
domU-12-34-67-89-01-C9.usma2.compute.amazonaws.comssh_key_namestagingaws_groups
...
=========================
Question :

Is there a way of accessing a specific key, for example : aws_status ?
If so, how do I do that ?

Thank you so much.

Regards.
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2009-01-19 18:46
(Received via mailing list)
On Jan 19, 2009, at 12:00 PM, Fernando Ramos wrote:

>     :aws_instance_id    => "i-123f1234",
>     :aws_availability_zone => "us-east-1b",
> =========================
> If so, how do I do that ?
>
> Thank you so much.
>
> Regards.
>

You can:
  require 'pp'
  pp @ec2.describe_instances

or just use #inspect: (which is what irb does with the last expression
anyway)
  p @ec2.describe_instances
  puts @ec2.describe_instances.inspect

As for getting the specific key (did you mean 'aws_state'?), perhaps
something like:
   @ec2.describe_instances.each do |instance_hash|
     puts "%20s:
%s"%instance_hash.values_at(:aws_instance_id, :aws_state)
   end

           i-123f1234: running

You can look at: String#%, Kernel.sprintf, Hash#values_at, and Hash#[]
(oh yeah, and Enumerable#each) for the details.

-Rob

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
B46cf7a3b580f672bfb8b8e2aed6d6f1?d=identicon&s=25 Fernando Ramos (ramosbrazil)
on 2009-01-19 18:56
Rob Biedenharn wrote:
> On Jan 19, 2009, at 12:00 PM, Fernando Ramos wrote:
>
>>     :aws_instance_id    => "i-123f1234",
>>     :aws_availability_zone => "us-east-1b",
>> =========================
>> If so, how do I do that ?
>>
>> Thank you so much.
>>
>> Regards.
>>
>
> You can:
>   require 'pp'
>   pp @ec2.describe_instances
>
> or just use #inspect: (which is what irb does with the last expression
> anyway)
>   p @ec2.describe_instances
>   puts @ec2.describe_instances.inspect
>
> As for getting the specific key (did you mean 'aws_state'?), perhaps
> something like:
>    @ec2.describe_instances.each do |instance_hash|
>      puts "%20s:
> %s"%instance_hash.values_at(:aws_instance_id, :aws_state)
>    end
>
>            i-123f1234: running
>
> You can look at: String#%, Kernel.sprintf, Hash#values_at, and Hash#[]
> (oh yeah, and Enumerable#each) for the details.
>
> -Rob
>
> Rob Biedenharn    http://agileconsultingllc.com
> Rob@AgileConsultingLLC.com

Hi Rob,

You've got it !!! Thank you so much. It is working the way I want now !
Appreciate it !

Regards,

Fernando.
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2009-01-19 20:01
(Received via mailing list)
Fernando Ramos wrote:

> puts @ec2.describe_instances
>
> the results comes like this :
> =========================
> aws_image_idami-e444444daws_reasonaws_state_codeaws_owner000000000888aws_instance_id
> i-123f1234aws_reservation_idr-aabbccddaws_staterunningdns_name
> domU-12-34-67-89-01-C9.usma2.compute.amazonaws.comssh_key_namestagingaws_groups

puts outputs strings, so it calls .to_s on its argument.

.to_s on a hash calls .to_s on all its keys and values, and mashes them
together.

Use p @ec2.describe_instances, or require 'pp' and use pp.

> Is there a way of accessing a specific key, for example : aws_status ?
> If so, how do I do that ?

p @ec2.describe_instances[2][:aws_status]

Now play around with (and read the docs on) Array and Hash!
This topic is locked and can not be replied to.