Perl Experts. I have a problem and I need an efficient solution. I have been struggling with it and any help is much appreciated.

I have n number of hierarchical devices and need to create a relationship between them based on some simple rules.

I have an list called TOP with its elements TOP01 TOP02 TOP03

I have another list called MID with its elements MID01 to MID24

I have list called LOW with its elements LOW1 to LOW 27, the elements have associated number of devices.

Based on associated number of devices of LOW list assign LOW elements to MID and MID elements to TOP element following the rules.

The rules are TOP cant exceed 650000 devices

MID cant exceed 35000 devices

Here is my incomplete code.

#! /usr/bin/perl # # my @relation_Arr; my @toplevelArr = qw(TOP01 TOP02 TOP03); my @midlevelArr = qw(MID01 MID02 MID03 MID04 MID05 MID06 MID07 MID08 M +ID09 MID10 MID11 MID12 MID13 MID14 MID15 MID16 MID17 MID18 MID19 MID2 +0 MID21 MID22 MID23 MID24); my %lowlevelH = qw(LOW01 19000 LOW02 19523 LOW03 13456 LOW04 18992 LOW +05 19560 LOW06 18945 LOW07 19855 LOW08 12344 LOW09 17400 LOW10 18990 +LOW11 14566 LOW12 17896 LOW13 19000 LOW14 1 9523 LOW15 13456 LOW16 18992 LOW17 19560 LOW18 18945 LOW19 19855 LOW20 + 12344 LOW21 17400 LOW22 16897 LOW23 19877 LOW24 18676 LOW25 13457 LO +W26 12345 LOW27 18456); my $tottopSum = 0; my $totmidSum = 0; while (1) { foreach my $lowLevel (sort keys %lowlevelH) { if($tottopSum == 0) { $tottopSum = $tottopCount + $lowlevelH{$lowLevel}; $totmidSum = $totmidSum + $lowlevelH{$lowLevel}; push(@relation_Arr, "$toplevelArr[0]:$tottopSum, $midlevelArr[ +0]:$totmidSum, $lowLevel, $lowlevelH{$lowLevel}"); ### REmove the first element from the array toplevelArr and mi +dlevelArr next; } if (($tottopSum + $lowlevelH{$lowLevel}) < 650000) { if (($totmidSum + $lowlevelH{$lowLevel}) < 35000) { $tottopSum = $tottopCount + $lowlevelH{$lowLevel}; $totmidSum = $totmidSum + $lowlevelH{$lowLevel}; push(@relation_Arr, "$toplevelArr[0]:$tottopSum, $midlevel +Arr[0]:$totmidSum, $lowLevel, $lowlevelH{$lowLevel}"); ### Remove the first element from the array midlevelArr } else { #### Go down the list of lowLevel Hash and find a value th +at can #### added to $totmidSum such that it does not cross 35000 #### IF found add it to the result array relation_Arr #### Else assign it to the next mid level Element. } } else { ### Assign next TOP element of the array } } } print "@relation_Arr\n"; Results should be TOP01:19000 MID01:19000 LOW01 19000 TOP01:32456 MID01:32456 LOW03 13456 TOP01:51979 MID02:19523 LOW02 19523 TOP01:64323 MID02:31867 LOW08 12344 ;; ;; ;; and so on.

In reply to Build a relationship of devices from different lists by shawshankred

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.