Hello and welcome to Perl and the Monastery, Tigor!
Here are my suggestions on how to solve your problem:
- The problem with the code you showed is that m/^C/ requires an uppercase C at the beginning of the line, but the test data you've showed includes whitespace at the beginning of the line and the name starts with a lowercase c. You can either change the regular expression into m/^\s*C/i to allow for case-insensitive matching and whitespace at the beginning of the line, or you could also remove whitespace from the beginning of the line with $line=~s/^\s+//;. Yet another alternative is to match the name after the split that I describe below, because that will remove the whitespace for you, and then only compare the name itself.
- You can use split to split the line on whitespace and store it into an array. For example, my @fields = split ' ', $line;
- You can then use shift to remove the first field from an array. For example, my $first = shift @array;.
- You can use sum from List::Util to sum a list of numbers. For example, put use List::Util qw/sum/; at the top of your script, and then use my $sum = sum(@array);
I hope this is enough information to get you started. For a good introduction to Perl in general, see perlintro.
In addition, here are some stylistic tips:
- I'd suggest using a separate variable for the filename. For example, my $file = "test_scores.txt";, and later on you can declare $line in the loop with while( my $line=<FH> ).
- The more modern three-argument open and lexical filehandles are generally recommended. For example, open my $fh, '<', $file or die "$file: $!"; - then, use $fh instead of FH.