Hi wishartz, I have a standard approach for this kind of problem.

Just put host and service information in anonymous hash trees and join them at the end.

Checking for host or service existence is very straightforward (look at the die statements)

Hope it helps.

Regs.

use strict; use warnings; our $hosts={}; $hosts->{hosta}->{ip}=192.168.1.1; $hosts->{hostb}->{ip}=192.168.1.2; $hosts->{hostc}->{ip}=192.168.1.3; our $services={}; $services->{1}->{ID}=1; $services->{1}->{name}='servicea'; $services->{1}->{startup}=sub { print "--> start service 1\n" }; $services->{2}->{ID}=2; $services->{2}->{name}='serviceb'; $services->{2}->{startup}=sub { print "--> start service 2\n" }; $hosts->{hosta}->{services}->{1}=$services->{1}; $hosts->{hostb}->{services}->{1}=$services->{1}; $hosts->{hostc}->{services}->{2}=$services->{2}; print "--> type hostname\n"; my $host=<STDIN>; chomp($host); die "host unknown " unless exists $hosts->{$host}; print "--> which service do you want to use? (Type ID)\n"; while (my ($ID, $service) = each %{$hosts->{$host}->{services}}) { print "- ID $ID, name ".$service->{name}."\n"; } my $ID=<STDIN>; chomp($ID); die "service unknown " unless exists $hosts->{$host}->{services}->{$ID}; &{$hosts->{$host}->{services}->{$ID}->{startup}}; exit; ---> output: --> type hostname hosta --> which service do you want to use? (Type ID) - ID 1, name servicea 1 --> start service 1

In reply to Re: Question about data structures by DACONTI
in thread Question about data structures by wishartz

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.