Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Anonymous hash reference

Status
Not open for further replies.

job357

Technical User
Sep 16, 2002
106
US
I am having some troubles accessing complex data structures hash values, give the script below and data structure, can someone assist in accessing the keys and values?

#!/usr/bin/perl -wT

use Data::Dumper;
use FindBin qw($Bin);
use XML::Simple;
use strict;

my ($data,$xml,$VAR1);
#$xml = new XML::Simple (KeyAttr=>'sku', forcearray=>'1');
$xml = new XML::Simple (forcearray=>'1');
$data = $xml->XMLin("$Bin/bmc.xml");
print Dumper($data);
print "\n";

output:

$VAR1 = {
'JOB' => [
{
'MAXRUNS' => '0',
'APPLICATION' => 'STICST',
'MAXDAYS' => '0',
'MAR' => '1',
'TASKTYPE' => 'Job',
'FEB' => '1',
'NOV' => '1',
'INTERVAL' => '0M',
'DATACENTER' => 'EM613',
'CONFIRM' => '0',
'MAY' => '1',
'TIMETO' => '0105',
'OCT' => '1',
'QUANTITATIVE' => [
{
'QUANT' => '1',
'NAME' => 'SYSTEM'
},
{
'QUANT' => '1',

 
XML::Simple returns a hash, and forcearray makes each hash entry point to an array. If you don't use this, it only generates arrays when the XML has multiple occurrences of the same thing, which can make it pretty tricky to figure out. So in your example
Code:
 print "$data{'JOB'}->[0]->{'QUANTITATIVE'}->[1]->{'QUANT'}\n";
prints the value of 'QUANT' in the last line of your Dumper output, i.e. 1
 
I'm not sure exactly which keys you're trying to access. Here's two examples that'll retrieve something useful anyway.

$data->{JOB}->[0]->{MAXRUNS}
$data->{JOB}->[0]->{QUANTITATIVE}->[0]->{NAME}
 
Thanks everyone!

What I wanting to is somehow get a list of the keys in the hash array (am I saying this right) so that I can change their values throughout the xml document: I tried the following for obtaining all the keys in the document:

#!/usr/bin/perl -wT

use Data::Dumper;
use FindBin qw($Bin);
use XML::Simple;
use strict;

my ($runs, @maxruns, $key, @values, $xml_file, $data);

$xml_file = "$Bin/bmc.xml";
$data = XMLin($xml_file, forcearray=>1);
@maxruns = qw(MAXRUNS);
$runs = \@maxruns;

foreach $key (keys %$data) {
print "The key is $key and value $$data{$key}\n";
}

Is there a better approach for doing this?
 
The problem is that you have a reference to a top level hash with only one key, JOB. This is a reference to an anonymous array, the entry(ies) of which are references to other anonymous hashes, which in turn... well, you get the picture.

In order to 'walk' the whole tree, you need a recursive function that can tell the difference beween an array reference, a hash reference, and a scalar value, in much the same way that Data::Dumper can. I suspect it probably uses the ref function.

BTW, the last code I posted was wrong, should be
Code:
print "$data[red]->[/red]{'JOB'}->[0]->{'QUANTITATIVE'}->[1]->{'QUANT'}\n";
I see ishnid didn't make the same mistake...[blush]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top