Hello Monks, Apologies if this has been asked before, but I am having trouble deciding on the best way to represent a certain data structure in perl.
Basically I want to store the layout of our network (read from a text file generated by another script) It looks something like this:
Main Switch Port
01/01 -> router
01/01 -> server1
01/01 -> hpswitch1 -> port1 -> workstation1
01/01 -> hpswitch1 -> port2 -> workstation2
01/01 -> hpswitch1 -> port3 -> workstation3
01/02 -> hpswitch2 -> port1 -> server 4
01/02 -> hpswitch2 -> port2 -> server5
01/03 -> server2
02/01 -> server3
02/02 -> hpswitch3 -> port1 -> workstation4
02/02 -> hpswitch3 -> port2 -> workstation5
etc...

So the entries like 01/01 are the main switch blade and port numbers, and hanging off those are other servers, or other switches with more ports and more things hanging off them. At the moment I am representing the data like this

For items that aren't switches
%network{$bladeAandPort}{$item} = $host

e.g.
01/01 -> item1 = router
01/01 -> item2 = server1

and for the switches I am nesting another hash inside like this...
%network{$bladeAandPort}{$item}{$switch}{$switchport}

e.g.
01/01 -> item3 -> hpswitch1 -> port01 = workstation1

However, when I come to iterate through all these, I need a way to say:

If the 3rd item in the data structure is a string, then it's a machine name, otherwise it's a ref to another hash, i'e it's a switch. So how do I determine if a value of a hash key is a string or a ref to another hash?

Additionally, is this the best way to represent this kind of stuff. This data map will be changing quite regularly, so I will have to remake the map adding or delete hashes as things get moved around.

The aim of this code will be to compare a stored version of this data with the current version so we get notified of any changes made to the network, E.g. "workstation1 has been unplugged from hpswitch1."

Am I going about this the best way? Hope this is kind of clear, any help greatly appreciated

Nick

In reply to generating hashes of hashes by mrnick1234567

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.