Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Accessing/Printing Complex Data Structure

by blink (Scribe)
on Apr 11, 2003 at 17:00 UTC ( #249928=perlquestion: print w/ replies, xml ) Need Help??
blink has asked for the wisdom of the Perl Monks concerning the following question:

I'm having a hard time accessing and printing variables contained in the data structure below.

Specifically, what I'm looking for out of this is:

key = intra-d_1050044970
frag1 media ID = DA0006
frag2 media ID = DA0006
image kilobytes = 713865 image number_of_files = 143

I've tried the following; as you can see, I'm fairly lost in this problem:

#!/opt/openv/perl/bin/perl -w use strict; use Data::Dumper; use bpimagelist; my %images = sls_bpimagelist(); #print Dumper(\%images); foreach my $img ( sort keys %images ) { foreach ( @{$images{$img}} ) { print "key = $img"; foreach ( @{$images{$img}{'media_id'}} ) { print "ID = $_\n"; } } }
This returns:

Not an ARRAY reference at /opt/openv/scripts/orapt.pl line 11.

Can anyone help me unwind this data structure?

Thx, Steve

'intra-d_1050044970' => { 'frags' => [ [ { 'media_id' => 'DA0006', 'density' => '21', 'flags' => '0', 'file_num' => '31', 'i_unused1' => '0', 'mpx' => '1', 'i_unused2' => '0', 'i_unused3' => '65539', 'block_size' => '65536', 'f_unused1' => '*NULL*', 'frag_num' => '1', 'media_date' => '1049782622', 'host' => 'bkup1', 'remainder' => '0', 'copy_num' => '1', 'expiration_time' => '1052723370', 'offset' => '1426484', 'kilobytes' => '168384', 'dev_written' => '8' }, { 'media_id' => 'DA0006', 'density' => '21', 'flags' => '0', 'file_num' => '32', 'i_unused1' => '0', 'mpx' => '0', 'i_unused2' => '0', 'i_unused3' => '0', 'block_size' => '65536', 'f_unused1' => '*NULL*', 'frag_num' => '2', 'media_date' => '1049782622', 'host' => 'bkup1', 'remainder' => '512', 'copy_num' => '1', 'expiration_time' => '0', 'offset' => '1434968', 'kilobytes' => '545481', 'dev_written' => '8' } ] ], 'histo' => { 'f1' => '-1', 'f2' => '-1', 'f3' => '-1', 'f4' => '-1', 'f5' => '-1', 'f6' => '-1', 'f7' => '-1', 'f8' => '-1', 'f9' => '-1', 'f10' => '-1' }, 'image' => { 'class_type' => '0', 'mpx' => '1', 'schedule_type' => '0', 'proxy_client' => '*NULL*', 'keyword' => '*NULL*', 'number_of_frags' => '2', 'retention_level' => '3', 'elapsed_time' => '146', 'tir_expiration' => '0', 'request_id' => '0', 'blob_inc_full_time' => '0', 'kilobytes' => '713865', 'dump_level' => '0', 'db_compressed' => '0', 'creator' => 'root', 'software_versoin' => '6', 'image_type' => '0', 'ext_security_info' => '0', 'use_file_system' => '0', 'encrypted' => '0', 'client' => 'miidb1-d', 'input_options' => '0', 'object_descriptor' => '*NULL*', 'primary_copy' => '1', 'backup_status' => '0', 'tir_info' => '0', 'backup_id' => 'intra-d_1050044970', 'class' => 'intra-d_misc_os', 'number_of_files' => '143', 'sw_version' => '*NULL*', 'backup_time' => '1050044970', 'prev_blob_inc_time' => '0', 'sched_label' => 'Full', 'compressed' => '0', 'expiration_time' => '1052723370', 'name1' => '*NULL*', 'number_of_copies' => '1' } },

Comment on Accessing/Printing Complex Data Structure
Select or Download Code
Re: Accessing/Printing Complex Data Structure
by jasonk (Parson) on Apr 11, 2003 at 17:14 UTC
    foreach my $img ( sort keys %images ) { my $kilobytes = 0; my $file_num = 0; print "key = $img\n"; my $count = 1; foreach ( @{$images{$img}->{frags}} ) { foreach(@{$_}) { $kilobytes += $_->{kilobytes}; $file_num += $_->{file_num}; print "frag".$count++." media ID = $_->{media_id}\n"; } } print "image kilobytes = $kilobytes "; print "image number_of_files = $file_num\n"; }

    We're not surrounded, we're in a target-rich environment!
Re: Accessing/Printing Complex Data Structure
by graff (Chancellor) on Apr 12, 2003 at 03:19 UTC
    Given this bit of your code (note: indenting helps):
    foreach my $img ( sort keys %images ) { foreach ( @{$images{$img}} ) { # this is line 11 ...
    and this bit of output from Data::Dumper:
    'intra-d_1050044970' => { 'frags' => [ ....
    The problem is that your second "foreach" is treating the content of "$images{$img}" as a reference to an array, when it is actually a reference to a hash (and the first key of that hash, as shown by Dumper, is 'frags'). The code should have been:
    foreach my $img ( keys %images ) { foreach my $typ ( keys %{$images{$img}} ) { ... # next layer -- content of $images{$img}{$typ} -- depends o +n value of $typ: ... # 'frac' has AoAoH ... # others have just a hash
    Hope that helps.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://249928]
Approved by Tomte
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2015-07-07 10:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (88 votes), past polls