Noah wrote:
I am trying to figure out the best variable structure to contain the following data. and then also routines to extract it. Note there are 3-6 elements for the link_int associated with each network device. the Network devices are partitioned between the braces.

    {
      hostname => ['core1',],
      loopback => [250,],
      link_int => ['int1', 'int2', 'int3', 'int4', 'int5', 'int6',],
      link_ip => [37, 33, 29, 25, 21, 2,],
    },
    {
      hostname => ['core2',],
      loopback => [254,],
      link_int => ['int1', 'int2', 'int3',],
      link_ip => [34, 54, 10, ],
    },
    {
      hostname => ['core3',],
      loopback => [252,],
      link_int => ['int1', 'int2', 'int3', 'int4', 'int5', 'int6',],
      link_ip => [57, 49, 45, 41, 30, 53,],
    };

Well, if you remove the trailing semicolon, you can simply assign the whole thing to an AoHoA ("array of hashes of arrays"). Assuming that you name the array @devices, to get the link_int values for core3 you can say:

    print "$_\n" for @{ $devices[2]->{link_int} };

Of course, then you need to know that core3 is the 3rd element, which may be a disadvantage with that structure. An advantage may be that the devices appear in a fixed order.

With the below structure - a HoHoA - you can extract values more easily IMO:

    my %hostnames = (
      core1 => {
        loopback => [250,],
        link_int => ['int1', 'int2', 'int3', 'int4', 'int5', 'int6',],
        link_ip => [37, 33, 29, 25, 21, 2,],
      },
      core2 => {
        loopback => [254,],
        link_int => ['int1', 'int2', 'int3',],
        link_ip => [34, 54, 10, ],
      },
      core3 => {
        loopback => [252,],
        link_int => ['int1', 'int2', 'int3', 'int4', 'int5', 'int6',],
        link_ip => [57, 49, 45, 41, 30, 53,],
      },
    );

This is how you now can get the link_int values for core3:

    print "$_\n" for @{ $hostnames{core3}->{link_int} };

Suggested reading:

http://perldoc.perl.org/perldsc.html

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to