#!/usr/bin/perl -w use strict; open(PS, "ps aux|") || die "Can`t open PS filehand: $!\n"; my $strindex; #still a global var... argh! my %prochash=(); while ( ) { #obtain the length to match according to ps aux { print "#Enter the block just once\n"; last if $strindex; $strindex = index($_, "STAT"); #obtain how characters the RegEx should not match } #no more of this besides the first line if ( /^.{$strindex}(?.{1,4})\s){1}?/i ) { $prochash{$+{stats}}++; #count processes status } } close(LS); #or don`t if you want to maintain the PSes while (my($k, $v) = each(%prochash) ) { print "$k is $v times \n"; }