Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Using Map and RegEx for data extraction

by ajose (Acolyte)
on Oct 14, 2014 at 10:10 UTC ( [id://1103700]=perlquestion: print w/replies, xml ) Need Help??

ajose has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I am trying get a list of PIDs of perl.exe process. Following code is used for the same.
my @tasks = `tasklist /FI "IMAGENAME eq perl.exe" /NH /FO CSV`; my @pids; foreach my $task (@tasks) { my @list = split /,/, $task; map {$_ =~s/"//g} @list; push @pids, $list[1]; } print "@pids";
The above code works just fine. But I wonder there is any shorthand method to extract the same with a single line. I am aware of Win32::Process::List module which somehow not compatible with my Perl version. I tried using nested map which I am not able to get proper output. Any help is appreciated.

Replies are listed 'Best First'.
Re: Using Map and RegEx for data extraction
by jonadab (Parson) on Oct 14, 2014 at 10:42 UTC

    Hmm... let's see... If I were going to golf this down... I think the first thing I'd want to do would be to get rid of a bunch of unnecessary temporary variables and just let the results flow, using the return value from one thing as the argument for the next (i.e., functional paradigm). In this case, this would probably also mean turning the foreach loop into a map. Perhaps something like this (untested, because I'm not on MS Windows here):

    print map { my @list = split /,/, $_; map {s/"//g} @list; $list[1]; } `tasklist /FI "IMAGENAME eq perl.exe" /NH /FO CSV`;

    Inside the map, you've already got a map, but you're using it like another foreach loop, which is weird. But anyway, I'd do basically the same thing inside of there. For example, we can do without the @list variable.

    print map { (map {s/"//g} split /,/, $_)[1] } `tasklist /FI "IMAGENAME eq perl.exe" /NH /FO CSV`;

    That's all purely mechanical, changes that can be made more or less automatically, without thinking very hard about what the code's doing. If I wanted to go much further, I'd have to slow down a bit probably and actually know what's going on, maybe get a Microsoft Windows system and look at the output of tasklist so I can see what's being parsed, etc.

Re: Using Map and RegEx for data extraction (sub)
by Anonymous Monk on Oct 14, 2014 at 10:32 UTC

    The above code works just fine. But I wonder there is any shorthand method to extract the same with a single line

    Sure, its called a subroutine

    print join ' ', win32perlProcPids(), "\n"; sub win32perlProcPids { my @tasks = `tasklist /FI "IMAGENAME eq perl.exe" /NH /FO CSV`; my @pids; foreach my $task (@tasks) { my @list = split /,/, $task; s/"//g for @list; push @pids, $list[1]; } return @pids; }
Re: Using Map and RegEx for data extraction
by GotToBTru (Prior) on Oct 15, 2014 at 22:23 UTC

    Something similar we're using at present.

    We have 12 processes running on a remote Windows server, each started by a batch file. I added a line to each batch file, right after the line that invokes the actual process, to set the window title to "Doornail path/batchfile"; this identifies a process that has aborted. The perl looks for Doornail in the tasklist, kills the aborted ("zombie") window, and uses the second part of the window title to restart it. The loop is to limit how long this runs; I don't quite trust it to run on its own yet.

    use strict; use warnings; my $looper = 0; my $qtrs = shift || 4; while ($looper < (4*$qtrs)+1) { my $counter = 0; $looper += 1; my @zombies = grep { $_ =~ /Doornail/ } `tasklist /v`; foreach my $z (@zombies) { $counter += 1; my ($pid, $batch) = (split /\s+/,$z)[1,10]; my ($path,$cmd) = $batch =~ m:^(.+/)([^/]+)$:; `taskkill /pid $pid`; chdir "/applications/JEDI/$path"; $batch = "start $cmd"; system($batch) } printf "Loop %d, restarted %d processes\n",$looper,$counter; sleep 15*60; }

    Updated for clarity

    1 Peter 4:10

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1103700]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2024-04-18 03:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found