?
Solved

XML::Simple - Force array

Posted on 2009-04-12
4
Medium Priority
?
723 Views
Last Modified: 2012-05-06

Consider the data structure below.  The component A is a n array if there is more than one value.  
How do I use force array option to make A always be an array?

 ForceArray => [qw(A)] works for A, but its does not seem to work at all level.  E.g.  
ForceArray => [qw(C)]   does not work for C node.

How do I force array on C?

 


my $data =  {  'mydata' => {'A' => [{'end_date' => '2000-09-28','start_date' => '1999-12-14'},
                                                       {'end_date' => '2000-09-28','start_date' => '1999-12-14'} ]
                                           },
                                           {'B' => 'C' => 'D'}                                            },
                    }

Open in new window

0
Comment
Question by:cpeters5
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 24127288
 DB<46> x XMLin(XMLout({  'mydata' => {'A' => [{'end_date' => '2000-09-28','start_date' => '1999-12-14'} ] ,  C=>['B' ] }}),ForceArray=>[qw(A)])  
0  HASH(0x1ad3f34)
   'mydata' => HASH(0x1ad8ae8)
      'A' => ARRAY(0x1adbebc)
         0  HASH(0x19d0fe4)
            'end_date' => '2000-09-28'
            'start_date' => '1999-12-14'
      'C' => 'B'
                                                                                                                                                                                            DB<47> x XMLin(XMLout({  'mydata' => {'A' => [{'end_date' => '2000-09-28','start_date' => '1999-12-14'} ] ,  C=>['B' ] }}),ForceArray=>[qw(C)])  
0  HASH(0x1adb764)
   'mydata' => HASH(0x1ad6178)
      'A' => HASH(0x1acf9d0)
         'end_date' => '2000-09-28'
         'start_date' => '1999-12-14'
      'C' => ARRAY(0x1ad8b38)
         0  'B'
                                                                                                                                                                                            DB<48> x XMLin(XMLout({  'mydata' => {'A' => [{'end_date' => '2000-09-28','start_date' => '1999-12-14'} ] ,  C=>['B' ] }}),ForceArray=>[qw(A C)])
0  HASH(0x1ab9acc)
   'mydata' => HASH(0x1ae3ea8)
      'A' => ARRAY(0x1ad53a4)
         0  HASH(0x1ad828c)
            'end_date' => '2000-09-28'
            'start_date' => '1999-12-14'
      'C' => ARRAY(0x1aec3f4)
         0  'B'
                                           
0
 

Author Comment

by:cpeters5
ID: 24127413
Thanks ozo for a quick response.  But I ma not sure if I understand your result.
Let me phrase the question this way.

With the default option, I get the parsed data as
$data = {
              'keywords' => {
                                    'keyword' => 'X'
                                  },
              'sites' => {
                                  'site' => { 'address' => 'some value',
                                                  'city' => 'Mo Town'}
                                                }
                             }
               }

Now, what option do I use to make it looks like the following.
$data = {
              'keywords' => {
                                      [ 'keyword' => 'X']
                                  },
              'sites' => {
                                  'site' => [{ 'address' => 'some value',
                                                  'city' => 'Mo Town'
                                                }]
                             }
               }
0
 
LVL 84

Accepted Solution

by:
ozo earned 1500 total points
ID: 24127513
I assume you meant
              'keywords' => [
                                      { 'keyword' => 'X'}
                                  ],
since
              'keywords' => {
                                      [ 'keyword' => 'X']
                                  },
makes no sense


print Dumper(XMLin( <<END ,ForceArray=>[qw(keywords site)]));
<opt>
  <keywords>
    <keyword>X</keyword>
  </keywords>
  <sites>
    <site>
      <address>some value</address>
      <city>Mo Town</city>
    </site>
  </sites>
</opt>
END
0
 

Author Closing Comment

by:cpeters5
ID: 31569381
Thanks again!
pax
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question