The FM answer is pretty good. I just mentioned a couple of additional things:
You already have a hash with valid commands (which is a good idea). There is no need to duplicate this list in regular expression. I would do something like this:
if (my $routine = $command_table{$command}) { $routine->(@line); } else { print "Command must be: freq, length, density or quit\n"; }
Notice that I also pass @line to the subroutine because you will need this for the density command. Routines that take no arguments can simply ignore them.
You can also generate a list of valid commands for reporting an error using keys %command_table , but I will leave this as an exercise for you.
Another thing is that in the description of the input file column numbers are mentioned, which indicates that it has a fixed-width format. This is better analyzed using substr or unpack . If the field is always empty or contains a space, then your split will not parse it correctly. (If you are using substr , keep in mind that the number of columns starts at 0, when people often label the first column 1.)
cjm
source share