Вопрос

I have a program that parses some XML using XML::Simple. It appears at some point the XML has redundant entries in it that get serialized, but I can't for the life of me figure out how to get that data back as a simple string. There is a subset of this XML that appears that it is being put into an array by XML::Simple. The pertinent subsection is as follows when running a Dumper($data) where data is my parsed XML.

Audio = [
      {
        'AudioIndex' => {
                        'StartByteOffset' => '32768',
                        'DataSize' => '3071872'
                      },
        'SamplingRate' => '48000',
        'BitsPerSample' => '16',
        'AudioFormat' => 'MXF'
      },
      {
        'AudioIndex' => {
                        'StartByteOffset' => '32768',
                        'DataSize' => '3071872'
                      },
        'SamplingRate' => '48000',
        'BitsPerSample' => '16',
        'AudioFormat' => 'MXF'
      },
      {
        'AudioIndex' => {
                        'StartByteOffset' => '32768',
                        'DataSize' => '3071872'
                      },
        'SamplingRate' => '48000',
        'BitsPerSample' => '16',
        'AudioFormat' => 'MXF'
      },
      {
        'AudioIndex' => {
                        'StartByteOffset' => '32768',
                        'DataSize' => '3071872'
                      },
        'SamplingRate' => '48000',
        'BitsPerSample' => '16',
        'AudioFormat' => 'MXF'
      }
    ];

I pass a small subset of my XML parsed output to a variable like so:

my @audiostuff = $data->{ClipContent}->{EssenceList}->{Audio};

When I output the dump of this, i get the following

$VAR1 = [
      {
        'AudioIndex' => {
                        'StartByteOffset' => '32768',
                        'DataSize' => '3071872'
                      },
        'SamplingRate' => '48000',
        'BitsPerSample' => '16',
        'AudioFormat' => 'MXF'
      },
      {
        'AudioIndex' => {
                        'StartByteOffset' => '32768',
                        'DataSize' => '3071872'
                      },
        'SamplingRate' => '48000',
        'BitsPerSample' => '16',
        'AudioFormat' => 'MXF'
      },
      {
        'AudioIndex' => {
                        'StartByteOffset' => '32768',
                        'DataSize' => '3071872'
                      },
        'SamplingRate' => '48000',
        'BitsPerSample' => '16',
        'AudioFormat' => 'MXF'
      },
      {
        'AudioIndex' => {
                        'StartByteOffset' => '32768',
                        'DataSize' => '3071872'
                      },
        'SamplingRate' => '48000',
        'BitsPerSample' => '16',
        'AudioFormat' => 'MXF'
      }
    ];

So all seems right in the captured array. The array appears to have one element, and inside that one element there are four hashes. This is where I get stuck. I have no idea how to get the information in these hashes back out again. I've tried about 40 different things and I'm just banging my head against the wall. I'd love to either just pass the values of each hash into a simple local array (something like @samplingrate for example) where I have all four of these entries in it and can simply reference them by $samplingrate[0], etc.

Any help would be appreciated.

Это было полезно?

Решение

I believe you mean to be dereferencing the array, like so:

my @audiostuff = @{ $data->{ClipContent}->{EssenceList}->{Audio} };
# do stuff with $audiostuff[0], etc.

or just use an arrayref instead

my $audiostuff = $data->{ClipContent}->{EssenceList}->{Audio};
# do stuff with $audiostuff->[0], etc.

For some memorizable rules for dealing with references and data structures, see http://perlmonks.org?node=References+quick+reference.

Другие советы

my @audiostuff = $data->{ClipContent}->{EssenceList}->{Audio};

You will have to loop through your array ex: using for.

Below is how you can access it

    $audiostuff[0]->{'AudioIndex'}  # This will give you hashref

     # Below will give you value 32768, Similarly you can get value for 'DataSize' 

    $audiostuff[0]->{'AudioIndex'}->{'StartByteOffset'} 

    # To access other elements/values.

     $audiostuff[0]->{'SamplingRate'}

     $audiostuff[0]->{'BitsPerSample'}

     $audiostuff[0]->{'AudioFormat'} 

     # How it works is,

    $audiostuff[0] # is first element of array, 

    $audiostuff[0]->{'AudioIndex'} # -> is used because element we get 
above is hash reference and then we get value at key 'AudioIndex'.

     # Similarly for 

$audiostuff[0]->{'AudioIndex'}->{'StartByteOffset'} 
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top