Sorting a list by Secondary Key

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • joeke3el
    New Member
    • Mar 2007
    • 2

    Sorting a list by Secondary Key

    Hi everyone,

    I have not touched Perl in the last 4 years, my books are at work and I have something here I'm struggling to figure out. From a known list of servers, I need to gather up how many clearcase views are on each server and then only choose the one that has the least number of views that given day. This is what I have so far.

    my @servers = qw (
    srv30bld4
    srv30bld5
    srv40bld2
    srv40bld3
    srv40bld4
    );
    foreach (@servers) {
    my @view = `$CT lsview -short -host $_ | wc -l`;

    I'm running the clearcase command "lsview" on each server and doing a wc so I know just how many views came back. The result I get is as follows.

    srv30bld4 92
    srv30bld5 0
    srv40bld2 58
    srv40bld3 3
    srv40bld4 2

    So now I know how many clearcase views are on each server but I only want the one with the lowest amount. I'm having difficulty sorting this list. Any help is appreciated.

    Thanks.
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    assuming @view is what needs sorting:

    Code:
    @view = ('srv30bld4 92','srv30bld5 0','srv40bld2 58','srv40bld3 3','srv40bld4 2');
    @view = map {$_->[0]} sort {$a->[1] <=> $b->[1]} map {chomp;[$_, /\s+(\d+)$/]} @view;
    print $view[0];
    but what if there are two or more servers with the same number of minimum views?

    Comment

    • joeke3el
      New Member
      • Mar 2007
      • 2

      #3
      Originally posted by KevinADC

      but what if there are two or more servers with the same number of minimum views?
      Thanks for taking the time to reply Kevin, I really appreciate this. I tried running this with more than one server set to the same number of views and it picks the one it comes across first in the list. This is fine as this code will be placed in a pre-existing script that people use to create views. We're just trying to prevent an imbalance of views across the five servers that are available. If there is a tie among the lowest number of views, as long as one of them gets picked during a given round that'll work.

      Again, thanks so much.

      Joe

      Comment

      • docsnyder
        New Member
        • Dec 2006
        • 88

        #4
        @joeke3el

        If you want to read more about the backgrounds of Kevin's solution, it's called the "Schwarztia n Transform".

        Greetz, Doc

        Comment

        Working...