sorting multiple arrays

Discussion in 'Perl Misc' started by René Scheibe, Nov 11, 2004.

  1. hi folks,
    i am a bit new to perl and cant get my problem solved.

    i do some network statistics which i have read into 4 arrays.
    1. @hosts: holding the hostnames or ips
    2. @in: bytes in
    3. @out: bytes out
    4. @total: bytes in + out
    so for one host i have an entry in every array with the same index.

    how can i now sort them?
    i want them to be ordered depending on @total.

    many thanks in advance
    rene
     
    René Scheibe, Nov 11, 2004
    #1
    1. Advertising

  2. René Scheibe wrote:
    > hi folks,
    > i am a bit new to perl and cant get my problem solved.
    >
    > i do some network statistics which i have read into 4 arrays.
    > 1. @hosts: holding the hostnames or ips
    > 2. @in: bytes in
    > 3. @out: bytes out
    > 4. @total: bytes in + out
    > so for one host i have an entry in every array with the same index.
    >
    > how can i now sort them?
    > i want them to be ordered depending on @total.


    Poor choice of data structure. Why four independant arrays which you have to
    keep in sync manually?
    A far better approach would be an array of hashes where each array element
    is (a pointer to) a hash containing four values: host, in, out, and total.
    Then it would be trivial to use sort() to sort that array.

    The way you implemented the data structure I'm kind of afraid you will have
    to implement your own sort. At least I don't see a straigh-forward way to
    leverage the build-in sort() function to read from one array and manipulate
    four arrays simultaneously.

    jue
     
    Jürgen Exner, Nov 11, 2004
    #2
    1. Advertising

  3. IMHO, this way:

    for (sort {$total[$a] <=> $total[$b]} (0 .. $#total)) {
    print join ("\t", $hosts[$_], $in[$_], $out[$_], $total[$_]),"\n"
    }

    check
    perldoc -f sort

    Regards,
    Andrew

    René Scheibe wrote:

    > hi folks,
    > i am a bit new to perl and cant get my problem solved.
    >
    > i do some network statistics which i have read into 4 arrays.
    > 1. @hosts: holding the hostnames or ips
    > 2. @in: bytes in
    > 3. @out: bytes out
    > 4. @total: bytes in + out
    > so for one host i have an entry in every array with the same index.
    >
    > how can i now sort them?
    > i want them to be ordered depending on @total.
    >
    > many thanks in advance
    > rene


    --
    Andrew
     
    Andrew Tkachenko, Nov 11, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    843
    Daniel T.
    Mar 28, 2006
  2. Philipp
    Replies:
    21
    Views:
    1,157
    Philipp
    Jan 20, 2009
  3. Replies:
    2
    Views:
    1,462
    James Kanze
    Jul 6, 2010
  4. Jason
    Replies:
    0
    Views:
    395
    Jason
    Oct 4, 2006
  5. Tom Kirchner

    sorting by multiple criterias (sub-sorting)

    Tom Kirchner, Oct 11, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    483
    Michael Budash
    Oct 11, 2003
Loading...

Share This Page