In article <[EMAIL PROTECTED]>, [EMAIL PROTECTED] (Jp) writes: >The object of the code below is to output a list of space seperated fields >with PID, username and process. The code generates te correct output. My >guess is that my perl code can be smaller. Who dares?
Don't care about smaller. Care about clearer. Sometimes the two go together. >#!perl ># ># Object: ># To output a tab separated list of PID, username and process ># for windows XP ># ># Prerequisites: ># 1) ActiveState Perl ># 2) Windows XP > >use warnings; >use strict; > >for my $line (`tasklist /v /nh`) { > chomp($line); > if ( $line ne "" ) { Avoid creating a whole new block by instead writing: $line ne "" or next; > # extract PID > my $pid = substr($line, 26, 6); > # remove leading spaces > $pid =~ s/^ *([0-9]+)$/$1/g; Why check for digits? What's going to go wrong if you don't? > # extract username > my $user = substr($line, 88, 50); > # remove trailing spaces > $user =~ s/^(.*\S)\s+$/$1/g; > # change spaces in username to underscores > $user =~ s/\s/\_/g; Why? > # extract process > my $proc = substr($line, 0, 24).substr($line, 152, 72); > # change multiple spaces to single spaces > $proc =~ s/\s\s\s*/ /g; This is usually written as s/\s+/ /g. even though technically there is a redundancy there. Or if you really mean 'space' and not 'white space', you can do: $proc =~ tr/ //s; > # remove trailing space > $proc =~ s/\s$//g; > # remove trailing N/A > $proc =~ s/ N\/A$//g; > > # print tab seperated fields > print $pid, " ", $user, " ", $proc, "\n"; I don't see any tabs there. Try this: print "$pid\t$user\t$proc\n"; > } >} It is better design to have a subroutine that returns the values you want than to print them so close to having figured them out. One day you may want to do something other than printing them and you would like to be able to use the same code. -- Peter Scott http://www.perldebugged.com/ *** NEW *** http://www.perlmedic.com/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>