Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3234324
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:22:41+00:00 2026-05-17T17:22:41+00:00

I did a exercise like this, how do I calculate the # of XML

  • 0

I did a exercise like this, how do I calculate the # of XML elements collapsed into an array by XML::Simple so I don’t have to hard-code the # of elements?
I plan to use the code to parse a bigger xml file. I don’t want to cout the elements by manual.

Can I use some count to replace the magic numbers, a little like person.count or hobbie.length etc. As I know, I can use this kind of statement in C# conveniently.

#!/usr/bin/perl -w
use strict;
use XML::Simple;
use Data::Dumper;

my $tree = XMLin('./t1.xml');

print Dumper($tree);
print "\n";
for (my $i = 0; $i < 2; $i++) # magic number '2'
{
    print "$tree->{person}->[$i]->{first_name} $tree->{person}->[$i]->{last_name}\n";
    print "\n";
    for (my $j = 0; $j < 3; $j++) # magic number '3'
    {
        print $tree->{person}->[$i]->{hobbie}->[$j], "\n";
    }
    print "\n";
}

Out put:

could not find ParserDetails.ini in C:/Perl/site/lib/XML/SAX
$VAR1 = {
          'person' => [
                      {
                        'hobbie' => [
                                    'bungy jumping',
                                    'sky diving',
                                    'knitting'
                                  ],
                        'last_name' => 'Bloggs',
                        'first_name' => 'Joe'
                      },
                      {
                        'hobbie' => [
                                    'Swim',
                                    'bike',
                                    'run'
                                  ],
                        'last_name' => 'LIU',
                        'first_name' => 'Jack'
                      }
                    ]
        };

Joe Bloggs

bungy jumping
sky diving
knitting

Jack LIU

Swim
bike
run

My Xml source file as below

<Document>
  <person>
    <first_name>Joe</first_name>
    <last_name>Bloggs</last_name>
    <hobbie>bungy jumping</hobbie>
    <hobbie>sky diving</hobbie>
    <hobbie>knitting</hobbie>
  </person>
  <person>
    <first_name>Jack</first_name>
    <last_name>LIU</last_name>
    <hobbie>Swim</hobbie>
    <hobbie>bike</hobbie>
    <hobbie>run</hobbie>
  </person>
</Document>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:22:42+00:00Added an answer on May 17, 2026 at 5:22 pm

    Since XML::Simple will produce an array for you, it’s easy to count its length.

    E.g. $tree->{person} is an array – or rather an array reference (make sure it’s one by using ForceArray option of XML::Simple even if there’s only 1 person).

    • You can get its length by first de-referencing it into an array itself (using @{} array de-reference): @{ $tree->{person} }

    • Then you use the resulting array in a scalar context which evaluates to the # of elements in the array (In other words, a.lenth/a.count functions in other languages translate to Perl idiom scalar(@a) were the scalar() function is optional if scalar context already applies).

      In this case, the numeric comparison operator "<" will force the scalar context, but if that was not the case you could have used scalar() function.

    Example:

    # Don't forget ForceArray option of XML::Simple to ensure person and hobbie are array refs
    for (my $i = 0; $i < scalar( @{ $tree->{person} } ); $i++) { # scalar() is optional here
        print "$tree->{person}->[$i]->{first_name} $tree->{person}->[$i]->{last_name}\n";
        print "\n";
        for (my $j = 0; $j < @{ $tree->{person}->[$i]->{hobbie} }; $j++) {
            print $tree->{person}->[$i]->{hobbie}->[$j], "\n";
        }
        print "\n";
    }
    

    As a note, a somewhat different method of counting the length of a Perl array is the $#a construct, which returns the index of the last element of the array – e.g. 1 less than the amount of elements in the array. I’m not aware of any performance difference between using the two approaches, so if you find both equally readable, use them as appropriate (e.g. if you need to get index of the last element, use $#a; if # of elements, use @a or scalar(@a) as needed).

    A good reference is Perl Data Structures Cookbook @perldoc

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Did you ever have to choose between WISA or LAMP at the beginning of
Did you ever had a bug in your code, you could not resolve? I
Recently I did a Java programming exercise successfully which was sent by a recruiting
Did anybody port the C source code of StricMath.exp (OpenJDK) to Java and made
I got this exercise at uni: Write a program that declares a shared integer
I am learning python atm, and was doing an exercise from this site --
I have made a custom trackbar control, mostly as an exercise. I know i
I built this method to find the longest word in an array, but I'm
Did somebody come up with this pattern so it is a famous and well-known
I did the following matrix multiplication benchmark in IronPython based on code here :

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.