in reply to transfer a array to a hash

Yes, by all means, let's take the regex first.
Using the special vars you do,

002: $processname=" $`";  # matches before string
003: $memory = $&  ;     # $& matches the string

may increase your cost-of-processing. See perlre at the "Warning" section of the "Capture Buffers" segment. As that doc now (as of 5.10 but perhaps earlier; certainly for 5.12) there's a bit of ambiguity about the cost of $` and $&:

WARNING: Once Perl sees that you need one of $&, $`, or $' anywhere in the program, it has to provide them for every pattern match. This may substantially slow your program. Perl uses the same mechanism to produ +ce $1, $2, etc, so you also pay a price for each pattern that contains capturing parentheses. (To avoid this cost while retaining the groupin +g behaviour, use the extended regular expression "(?: ... )" instead.) B +ut if you never use $&, $` or $', then patterns *without* capturing parentheses will not be penalized. So avoid $&, $', and $` if you can, but if you can't (and some algorithms really appreciate them), once you've used them once, use them at will, because you've already paid t +he price. As of 5.005, $& is not so costly as the other two.

Others, deeper into benchmarking, or perlguts, may be able to clarify, but based on the traditional warning against those special regex vars, you might chose ordinary captures:

#!/usr/bin/perl use strict; use warnings; use 5.012; # 948541 my @arr = <DATA>; for $_(@arr) { if ( $_ =~ /([a-z 0-9\+\.]+?)(\d{0,3},{0,1}\d{0,3},{0,1}\d{1,3}) K +/i ) { #1 my $processname = $1; my $memory = $2; say "$memory --- $processname"; } } #1 NB this regex allows for entries as small as 1K -- a condition # of which you might want to be aware. =head output: 1,788,180 --- disp+work.exe 3380 Console 0 2,787,204 --- sqlservr.exe 1768 Console 0 1,078,120 --- jlaunch.exe 4608 0 1,830,376 --- sqlservr.exe 1812 0 488,716 --- disp+work.exe 4772 0 17 --- proc 9412 Console 0 =cut __DATA__ disp+work.exe 3380 Console 0 1,788,180 K sqlservr.exe 1768 Console 0 2,787,204 K jlaunch.exe 4608 0 1,078,120 K sqlservr.exe 1812 0 1,830,376 K disp+work.exe 4772 0 488,716 K small_proc 9412 Console 0 17 K

Is your data sample correct, showing "Console" present in some entries and not in others?

Replies are listed 'Best First'.
Re^2: transfer a array to a hash
by Marshall (Canon) on Jan 18, 2012 at 22:45 UTC
    Correct! Using these special regex variables is not advised due to the performance decrease.

    When I wrote my code, I didn't look closely at the way that these lines were generated and how the @array was built.

    But to generate the @array, with this DATA, here is one way....

    #!/usr/bin/perl -w use strict; my @data; foreach my $line (<DATA>) { # anchor match to the end of string with $ # anchor match to the beginning of string with ^ # ignore the K and spaces at the end (includes \n) # get the numbers and commas right before the K # get this stuff in front ($before) # but ignore any leading spaces in the line # a "greedy" match will not be so "greedy" that it # won't allow the last part of the regex to match # so it is not necessary to constrain the .+ match here my ($before, $mem) = ($line =~ /^\s*(.+)\s+([\d,]+)\s*K\s*$/); $mem =~ tr/,//d; #delete commas in like 2,787,204 push @data, "$mem $before\n"; #adds the \n back in } print @data; =prints: 1788180 disp+work.exe 3380 Console 0 2787204 sqlservr.exe 1768 Console 0 1078120 jlaunch.exe 4608 0 1830376 sqlservr.exe 1812 0 488716 disp+work.exe 4772 0 17 small_proc 9412 Console 0 =cut __DATA__ disp+work.exe 3380 Console 0 1,788,180 K sqlservr.exe 1768 Console 0 2,787,204 K jlaunch.exe 4608 0 1,078,120 K sqlservr.exe 1812 0 1,830,376 K disp+work.exe 4772 0 488,716 K small_proc 9412 Console 0 17 K
Re^2: transfer a array to a hash
by BlackForrest (Initiate) on Jan 25, 2012 at 11:50 UTC

    Hi, yes the "console " entry appears if process is running under Windows Console process

      Hey,
      thanks to all, finally i could fix it..
      i renamed all $variales and came around some unclear assignments

      %memhash=@allprocesses; $anz=keys(%memhash); print "Pair of Hashes: $anz <br>\n"; foreach $memkey ( sort{$b<=>$a}keys %memhash){ $name = "$values$memhash{$memkey}"; if ($memkey <500000) { + # value less then 500.000 byte print "$memkey -- Name: $name \n"; } else { + # values above 500.000 byte in Red +print "<h4>$memkey </h4>-- Name: $name \n"; }