Anonymous Hash

Discussion in 'Perl Misc' started by quartet, Jan 28, 2005.

  1. quartet

    quartet Guest

    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.
     
    quartet, Jan 28, 2005
    #1
    1. Advertising

  2. quartet

    Paul Lalli Guest

    "quartet" <> wrote in message
    news:...
    > 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";


    <Dumper output of a hashref snipped>

    > 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
     
    Paul Lalli, Jan 28, 2005
    #2
    1. Advertising

  3. "quartet" <> wrote in news:1106922655.678839.250080
    @z14g2000cwz.googlegroups.com:

    > 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.
     
    A. Sinan Unur, Jan 28, 2005
    #3
  4. quartet wrote:

    >
    > 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?"
     
    Chris Mattern, Jan 28, 2005
    #4
  5. A. Sinan Unur wrote:

    > [...]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.
     
    Brian McCauley, Jan 28, 2005
    #5
  6. Brian McCauley <> wrote in news:ctdr5i$9fg$1
    @sun3.bham.ac.uk:

    > A. Sinan Unur wrote:
    >
    >> [...]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
     
    A. Sinan Unur, Jan 28, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Reporter
    Replies:
    3
    Views:
    496
    Mike Schilling
    May 12, 2007
  2. rp
    Replies:
    1
    Views:
    592
    red floyd
    Nov 10, 2011
  3. Srijayanth Sridhar
    Replies:
    19
    Views:
    674
    David A. Black
    Jul 2, 2008
  4. Replies:
    1
    Views:
    241
  5. Justin C
    Replies:
    1
    Views:
    202
    Justin C
    Oct 7, 2013
Loading...

Share This Page