If you have read the entire data into an array, it is simply a matter of starting with the appropriate array index. Using the scheme of opening up the output of the w program as a file, you need to read two lines from the start of the file and discard them. The output format of w is in fixed columns. We can use substr to pull it apart. Because the fields are not always present, regular expressions don't help much here.
Regular expressions do help here.
The second substitution looks for a space or a closing parentheses and deletes that followed by zero or more of any character. Now is the time to count the number of times each program is run. The associative array is made just for this purpose. Create an array to hold counts, with each item being indexed by the program name. We could use the Unix sort program, but Perl has a builtin sorting function as well.