PERL: Numbering a grid any repetitive way you need

    Date: 08/19/09 (IT Professionals)    Keywords: php

    I'm working on some Perl code (might get moved to PHP) which will be the basis for displaying server racks - and blade servers (blade servers, in the big picture, really are just mini-racks).

    The trick is, racks can be of different U heights, and blade servers could be numbered however the OEM wants (you'd /think/ horizontally, starting in the upper left).

    The parameters of the 'racks' will be stored in a db (ldap in this case), and I wanted the parameters to be human readable.

    So, I've written the following Perl code. There's testing output included, and for what this is, I don't see any need to remove it.

    Here's some sample output:


    TLV: 20 VI: -1
    PriOrder: Vertical
    PriOrderStart: Bottom
    SecOrderStart: Right
    U_Horz: 5
    U_Vert: 4
    
    HorzInc: -4
    VertInc: -1
    TopLeftVal: 20
    
    20      16      12      8       4
    19      15      11      7       3
    18      14      10      6       2
    17      13      9       5       1
    

    I'd /love/ to hear thoughts on optimizing (especially the large 'if' section) it.

    - chris


    $PriOrder="Horizontal";
    $PriOrderStart="Left";
    $SecOrderStart="Bottom";
    $U_Horz=5;
    $U_Vert=4;
    
    if ( $PriOrder eq "Horizontal" ) {
      if ( $PriOrderStart eq "Left" ) {
        $HorzInc=1;
        if ( $SecOrderStart eq "Top" ) {
          $TopLeftVal=1;
          $VertInc=$U_Horz;
          print "TLV: $TopLeftVal\tVI: $VertInc\n";
        }
        if ( $SecOrderStart eq "Bottom" ) {
          $TopLeftVal=$U_Horz * ( $U_Vert - 1 ) + 1;
          $VertInc=-$U_Horz;
          print "TLV: $TopLeftVal\tVI: $VertInc\n";
        }
      }
      if ( $PriOrderStart eq "Right" ) {
        $HorzInc=-1;
        if ( $SecOrderStart eq "Top" ) {
          $TopLeftVal=$U_Horz;
          $VertInc=$U_Horz;
          print "TLV: $TopLeftVal\tVI: $VertInc\n";
        }
        if ( $SecOrderStart eq "Bottom" ) {
          $TopLeftVal=$U_Horz * $U_Vert;
          $VertInc=-$U_Horz;
          print "TLV: $TopLeftVal\tVI: $VertInc\n";
        }
      }
    }
    
    if ( $PriOrder eq "Vertical" ) {
      if ( $SecOrderStart eq "Left" ) {
        $HorzInc=$U_Vert;
        if ( $PriOrderStart eq "Top" ) {
          $TopLeftVal=1;
          $VertInc=1;
          print "TLV: $TopLeftVal\tVI: $VertInc\n";
        }
        if ( $PriOrderStart eq "Bottom" ) {
          $TopLeftVal=$U_Vert;
          $VertInc=-1;
          print "TLV: $TopLeftVal\tVI: $VertInc\n";
        }
      }
      if ( $SecOrderStart eq "Right" ) {
        $HorzInc=-$U_Vert;
        if ( $PriOrderStart eq "Top" ) {
          $TopLeftVal=$U_Vert * ( $U_Horz - 1 ) + 1;
          $VertInc=1;
          print "TLV: $TopLeftVal\tVI: $VertInc\n";
        }
        if ( $PriOrderStart eq "Bottom" ) {
          $TopLeftVal=$U_Vert * $U_Horz;
          $VertInc=-1;
          print "TLV: $TopLeftVal\tVI: $VertInc\n";
        }
      }
    }
    
    print "PriOrder: $PriOrder\n";
    print "PriOrderStart: $PriOrderStart\n";
    print "SecOrderStart: $SecOrderStart\n";
    print "U_Horz: $U_Horz\n";
    print "U_Vert: $U_Vert\n";
    print "\n";
    print "HorzInc: $HorzInc\n";
    print "VertInc: $VertInc\n";
    print "TopLeftVal: $TopLeftVal\n\n";
    
    for ($VertLoop = 0; $VertLoop < $U_Vert; $VertLoop++) {
      $RowStart = $TopLeftVal+($VertLoop*$VertInc);
      $RowEnd   = $RowStart+$HorzInc*($U_Horz-1);
    
      for ($HorzLoop = $RowStart; $HorzLoop != $RowEnd+$HorzInc; $HorzLoop+=$HorzInc) {
        print "$HorzLoop\t";
      }
      print "\n";
    }
    

    Source: https://itprofessionals.livejournal.com/86255.html

« Windows 7 network failure... || It's the newest post


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home