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 | |
|
Re^2: transfer a array to a hash
by BlackForrest (Initiate) on Jan 25, 2012 at 11:50 UTC | |
by BlackForrest (Initiate) on Jan 25, 2012 at 15:04 UTC |