sorting multiple arrays

R

René Scheibe

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
 
J

Jürgen Exner

René Scheibe said:
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
 
A

Andrew Tkachenko

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top