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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.