0

I have the data structure (given below), can you please let me know how to populate and also to access the elements in the simplest form anytime I want. Which is the best way to implement, like Hash of Hashes or Array of Arrays or anything else ? There won't be too many elements in the array, the only problem is the its a multi-level kind of array

%Array = (

    'Item0' => {
        'Name' => 'Item0_Name',
        'Attribs' => {
            'Attr1' => {
                'Name' => 'Attr1_Name',
                'Num'   => 'Attr1_Num'
            },      
            'Attr2' => {
                'Name' => 'Attr2_Name',
                'Num'   => 'Attr2_Num'
            }       
        }       
    },

    'Item1' => {
        'Name' => 'Item1_Name',
        'Attribs' => {
            'Attr1' => {
                'Name' => 'Attr1_Name',
                'Num'   => 'Attr1_Num'
            },      
            'Attr2' => {
                'Name' => 'Attr2_Name',
                'Num'   => 'Attr2_Num'
            }       
        }       
    }

);
2
  • 4
    perldoc perldsc Commented Sep 4, 2012 at 6:43
  • Calling a hash Array is confusing. Commented Sep 4, 2012 at 7:40

1 Answer 1

2

The official resource for this is the Perl Data Structure Cookbook (perldoc perldsc).

For the data shown, it seems that an array of hashrefs is sufficient since the keys shown in the OP are simply a stringified index. The same comment applies for 'Attribs':

my @array = (
              {
                  'Name' => 'Item0_Name',
                  'Attribs' => [
                                  {
                                      'Name' => 'Attr1_Name',
                                      'Num'  => 'Attr1_Num'
                                  },      
                                  {
                                      'Name' => 'Attr2_Name',
                                      'Num'   => 'Attr2_Num'
                                  }       
                               ],
              },
              {
                  'Name' => 'Item1_Name',
                  'Attribs' => [
                                  {
                                      'Name' => 'Attr1_Name',
                                      'Num'  => 'Attr1_Num'
                                  },      
                                  {
                                      'Name' => 'Attr2_Name',
                                      'Num'   => 'Attr2_Num'
                                  }       
                               ],
              },
           );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.