Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Counting Elements output from Data::Dumper

by vbrtrmn (Pilgrim)
on Nov 09, 2002 at 01:30 UTC ( [id://211598]=perlquestion: print w/replies, xml ) Need Help??

vbrtrmn has asked for the wisdom of the Perl Monks concerning the following question:

Good evening my fine monks, please offer me some kind advice for my sometimes foolish questions.

I'm working with XML::Simple and Data::Dumper.

Both seem to work great, I can pass my xml file and I can get the dump w/ no problem.

My question is: "How can I count the number of elements in LOGENTRY, if the data is stored in a scalar rather than a hash?"
In my simplified version I can obviously tell that I have 4 elements, but in a more complex one I have more, say 80 or 150. I'd use a subroutime to print the 129th element if it exists (or whatever).

simplified script

#!/usr/bin/perl -w use strict; use XML::Simple; use Data::Dumper; my $file = "test.xml"; my $config = XMLin($file); print Dumper($config); exit;

simplified output

$VAR1 = { 'LOGENTRY' => [ { 'DATE' => 'Date 0', 'MEMO' => 'Memo 0', 'TITLE' => 'Test 0' }, { 'DATE' => 'Date 1', 'MEMO' => 'Memo 1', 'TITLE' => 'Text 1' }, { 'DATE' => 'Date 2', 'MEMO' => 'Memo 2', 'TITLE' => 'Text 2' }, { 'DATE' => 'Date 3', 'MEMO' => 'Memo 3', 'TITLE' => 'Text 3' } ] };

thanks
(not even close to a saint) paul

Replies are listed 'Best First'.
Re: Counting Elements output from Data::Dumper
by LTjake (Prior) on Nov 09, 2002 at 02:02 UTC
    print scalar @{$VAR1->{'LOGENTRY'}};
    gives me 4 as output. (see perlref)

    HTH.

    --
    Rock is dead. Long live paper and scissors!
      print scalar @{$VAR1->{'LOGENTRY'}};

      this returned an error due to the use of strict, but:

      print scalar @{$config->{'LOGENTRY'}};

      Returns: 4

      THANKS A LOT!!


      --
      paul
Re: Counting Elements output from Data::Dumper
by robartes (Priest) on Nov 09, 2002 at 12:50 UTC
    Your result is returned as a reference to an array (which contains references to hashes - the whole is an AoH: Array of Hashes). You need an extra dereferencing step to get to the array itself. As LTjake says above, to get the length of the referenced array, do something like:
    my $length=scalar @$config;
    $config contains a reference to an array, which you can access by dereferencing it: prefix it with an @ symbol. @$config or @{$config} literally means: the array referenced by $config. If $config refered to a hash, you'd use %$config to get it.

    Have a look at perlref for more information on references in Perl (which are somewhat like pointers in C, but then again, are not).

    CU
    Robartes-

      my $length=scalar @$config; print $length;

      Returns an Invalid Server Error

      my $length=scalar %$config; print $length;

      Returns 1/8, I'm really not sure what that refers to, can you give me some more enlightenment?


      --
      paul
        Hi,

        the reason you get an error on scalar @$config is that I goofed :). I missed a level in the data structure referenced to by $config: it's actually a HoAoH (hash of arrays of hashes). So, to get the length of LOGENTRY, you need to access the value of the LOGENTRY key in the hash referenced by $config, and all that in scalar context. In other words: do what LTjake said :).

        print scalar %$config This is funny - what's happening here is that you're accessing a hash in scalar context ($config is a hash reference, so %$config is a hash. scalar puts it in scalar context.). This gives you the bucket efficiency of the hash, which is pretty useless in your case.

        Sorry for the goof-up.

        CU
        Robartes-

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2024-03-28 23:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found