Hi community,
i have of cause read some examples to do what i want to do, but nothing helps ...
I would output some Server details - in this case Memory and Process-PID/Name in a loop and then ring them into a table in a "sorted" way...
Let's start with the pattern matching:
if ($line=~ /(\d{1,3},\d{3,3},\d\d\d K)/){ # e.g. 3,373,560 K $processname=" $`"; # matches before string $memory = $& ; # $& matches the string
if i tell them to print i this loop everything is fine
print "$memory --- $processname <br>\n";
1788180 --- disp+work.exe 3380 Console 0
2787204 --- sqlservr.exe 1768 Console 0
1078120 --- jlaunch.exe 4608 0
etc..
then i push them in to Array @allprocesses
push(@allprocesses, $memory => " - $processname <br>",); $memory = ""; # empty variable $processname = ""; # empty variable print "<td width=\'400\' height=\'15\'>Unsorted Array: <br> @allproce +sses </td> \n" ;
print out this is also still o.k.
Unsorted Array
1830376 - sqlservr.exe 1812 0
488716 - disp+work.exe 4772 0
350060 - disp+work.exe 9712 0
396548 - disp+work.exe 9980 0
327372 - disp+work.exe 7892 0
397132 - disp+work.exe 2872 0
276124 - disp+work.exe 8948 0
159176 - disp+work.exe 7292 0
283332 - disp+work.exe 9620 0
199288 - disp+work.exe 7760 0
119540 - disp+work.exe 6376 0
433064 - disp+work.exe 8712 0
255120 - disp+work.exe 4548 0
507316 - disp+work.exe 7852 0
104272 - disp+work.exe 10208 0
119728 - SavService.exe 2336 0
Now i want to sort this array, or better run this "pairs" sorted to play around like ">500000 then make something..."
%memhash=@allprocesses; $anz=keys(%memhash); print "Pair of Hashes: $anz <br>\n"; foreach $key ( sort {$b <=> $a} values %memhash){ $help1 = " $keys$memhash{$keys}"; print "Memory: $key Name: $help1 <br> \n";
"sort" works, but where are my processname ($help1) gone?
Output:
Pair of Hashes: 16
Memory: 1830376 Name: 1830376
Memory: 507316 Name: 1830376
Memory: 488716 Name: 1830376
Memory: 433064 Name: 1830376
Memory: 397132 Name: 1830376
Memory: 396548 Name: 1830376
Memory: 350060 Name: 1830376
Memory: 327372 Name: 1830376
Memory: 283332 Name: 1830376
Memory: 276124 Name: 1830376
Memory: 255120 Name: 1830376
Memory: 199288 Name: 1830376
Memory: 159176 Name: 1830376
Memory: 119728 Name: 1830376
Memory: 119540 Name: 1830376
Memory: 104272 Name: 1830376
Also try to sort with array from the hash:
@sorted = sort({$b<=>$a}values(%memhash)); for ($i=0; $i<=$#sorted; $i++) { my $paar = $sorted[$i]; print "$paar = $memhash{$paar} <br>" ; } print "</td></tr>\n" ;
output:
sorted Array:
1830376 =
507316 =
488716 =
433064 =
397132 =
396548 =
350060 =
327372 =
283332 =
276124 =
255120 =
199288 =
159176 =
119728 =
119540 =
104272 =
Can someone find the bug?
Thanks to all,
Klaus
In reply to transfer a array to a hash by BlackForrest
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |