Anonymous Hash

Q

quartet

I am using the following script to output the data structure below:

#!/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/some.xml");
print Dumper($data);
print "\n";

output:


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

Can someone tell me why its neccessary to use '%$' for referncing the
hash, I want to access all the values in the anonymous hash.
 
P

Paul Lalli

quartet said:
I am using the following script to output the data structure below:

#!/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/some.xml");
print Dumper($data);
print "\n";

Can someone tell me why its neccessary to use '%$' for referncing the
hash, I want to access all the values in the anonymous hash.

Because you don't have an anonymous hash. You have a reference to an
anonymous hash. $data is not a hash. It is a reference to a hash. In
order to get at the hash that $data references, you must dereference
$data. In perl, the way to dereference a reference is to precede it
with the character that represents the kind of variable. In this case,
'%' for hash.

$data ==> hash reference
%$data ==> hash that $data references

To access all the elements of the hash, you can do something similar to:

foreach (keys %$data){
print "Key $_ has value $data->{$_}\n";
}

(Note that '$data->{$_}' can just as effectively be written
'$$data{$_}'. The choice is yours, but I believe the arrow notation is
generally more well accepted).

Hope this helps,
Paul Lalli
 
A

A. Sinan Unur

I am using the following script to output the data structure below:

I am not sure what all this code has to do with your question, so I am
snipping:

....
my $xml = new XML::Simple (forcearray=>'1');
....

Can someone tell me why its neccessary to use '%$' for referncing the
hash, I want to access all the values in the anonymous hash.

XML::Simple returns a reference to a hash. You can _dereference elements
of the hash referred to by $xml by using the $xml->{JOBS} notation which
is basically syntatic sugar. It accomplish the same things as

%{ $xml }{JOBS}

in a visually more pleasing way.

For more information,

perldoc perlreftut

Sinan.
 
C

Chris Mattern

quartet said:
Can someone tell me why its neccessary to use '%$' for referncing the
hash, I want to access all the values in the anonymous hash.

Um, because that's how you dereference a reference to a hash. Whether
the reference was made by using an anonymous hash or a named hash is
irrelevant; that's how you dereference it.

--
Christopher Mattern

"Which one you figure tracked us?"
"The ugly one, sir."
"...Could you be more specific?"
 
B

Brian McCauley

A. Sinan Unur said:
[...]the $xml->{JOBS} notation which
is basically syntatic sugar. It accomplish the same things as

%{ $xml }{JOBS}

You mean:

${ $xml }{JOBS}

or, since $xml is a simple variable not an expression, it doesn't need
to be wrapped in a block:

$$xml{JOBS}
in a visually more pleasing way.

Yes I like the -> too.
 
A

A. Sinan Unur

A. Sinan Unur said:
[...]the $xml->{JOBS} notation which
is basically syntatic sugar. It accomplish the same things as

%{ $xml }{JOBS}

You mean:

${ $xml }{JOBS}

Yes, I did, thank you for catching that.
or, since $xml is a simple variable not an expression, it doesn't need
to be wrapped in a block:

$$xml{JOBS}

Clearly, however, I wanted to illustrate _what_ that expression was
dereferencing. Didn't quite work as I had intended.


Sinan
 

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

Similar Threads

XML::Simple drives me mad 9
XML Simple force array 4
Hash key types and equality of hash keys 2
XML::Simple 1
Is this a Hash and how can I test a value? 3
data to hash 1
In deep complex XML 0
In deep complex XML 5

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top