Since you need to do these in order, I'd probably start with an array. Further, I like to try to make things flexible so that I can easily adjust the behavior in the future. The following quick hack will iterate over the clusters. It defines the total number of machines per cluster (in case they change), and if any machine in the cluster has a different behavior, allows you to assign a subref to that machine. In the future, you can change the number of machines and change the behavior for any machine for any cluster to anything you want. Further, the data structure is very intuitive. This should be simple to maintain.

Note how easy it is to change the behavior for the fourth cluster.

use strict; use warnings; my @clusters = ( { total => 7, 7 => \&two }, { total => 7, 7 => \&two }, { total => 7, 7 => \&two }, { total => 16, 7 => \&two, 4 => \&three }, { total => 7, 7 => \&two }, { total => 7, 7 => \&two }, { total => 7, 7 => \&two } ); foreach my $cluster ( @clusters ) { # decrement count by one to account for arrays starting at zero my $machine_count = $cluster->{ total } - 1; for my $machine ( 0 .. $machine_count ) { if ( ! exists $cluster->{ $machine + 1 } ) { &one; } else { &{ $cluster->{ $machine + 1 } }; } } } sub one { print "First sub\n"; } sub two { print "Second sub\n"; } sub three { print "Third sub\n"; }

You'll probably want to play with that for loop as the increments and decrements look weird, but it's a start.

Cheers,
Ovid

Vote for paco!

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to (Ovid - plan for the future) Re: Data Structure Design by Ovid
in thread Data Structure Design by Tuna

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.