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

  • SEARCH
  • Home
  • 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 7068817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:22:12+00:00 2026-05-28T05:22:12+00:00

I am parsing xml files in Perl and everything seems to work great with

  • 0

I am parsing xml files in Perl and everything seems to work great with one issue. I have files with the same schema, but they are returning different types of data from the parser. Here is a simplified example:

<tests>
       <test>
          <data1>Hi</data1>
          <data2>Hello</data2>
       </test>
       <test>
          <data1>Hi2</data1>
          <data2>Hello2</data2>
       </test>
  </tests>

In a dump, this returns the following: (Take note of test being an array of two hashes)

$VAR1 = {
          'test' => [
                    {
                      'data2' => 'Hello',
                      'data1' => 'Hi'
                    },
                    {
                      'data2' => 'Hello2',
                      'data1' => 'Hi2'
                    }
                  ]
        };

Now, for a similar set of data, but with only one ‘test’ entity like so:

  <tests>
       <test>
          <data1>Hi</data1>
          <data2>Hello</data2>
       </test>
  </tests>

This returns similar data, EXCEPT the test entity is no longer an array, but a singular hash:

$VAR1 = {
          'test' => {
                    'data2' => 'Hello',
                    'data1' => 'Hi'
                  }
        };

My dilemma is that my code expects an array there, as that is the norm. But on the slim chance when there is only one entity, it will return a hash for that entity. My question is, how can I handle the hash entity as though it were an array. Or test for it?

Right now my code for retrieving the array is like this:

foreach $test (@{$data->{'tests'}->{'test'}})
{
   do something with $test
}

But with the hash, it gives an error “Not an ARRAY reference”. I hope this is enough detail! Thanks!!!

  • 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-28T05:22:13+00:00Added an answer on May 28, 2026 at 5:22 am

    Perhaps the alternate form of the ForceArray option is what you want?

    ForceArray => [ names ]

    This alternative (and preferred) form of the ‘ForceArray’ option
    allows you to specify a list of element names which should always be
    forced into an array representation, rather than the ‘all or nothing’
    approach above.

    It is also possible (since version 2.05) to include compiled regular
    expressions in the list – any element names which match the pattern
    will be forced to arrays. If the list contains only a single regex,
    then it is not necessary to enclose it in an arrayref. Eg:

    ForceArray => qr/_list$/

    So I might try:

    ForceArray => ['test']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on some code that deals with parsing files (mainly XML, but there
At work I am parsing large XML files using the DefaultHandler class. Doing that,
I'm relatively new to Perl, writing some XML parsing scripts. I have done two
I have moved into parsing XML files in WP7 and until now was finding
I have thousands of gz files, each gz file has one XML file compressed.
I have always been using jaxb for parsing XML files into java object. This
In My iphone application i have two XML files. I can parse one.xml eailsy
I'm looking for a pure Lua library for parsing XML files. It doesn't have
Following on from my recent question regarding parsing XML files in Java I have
New to XML parsing.I need to read a group of arbitrary XML files. They

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.