I am required to parse a file and sort all alphabetical characters according to frequency from highest to lowest. I am seriously stuck on the sorting and maybe there is a better way to do this. My code:

use strict; use warnings;

my $cnt = (); ### I don't know why I need this next line but strict complains if I don't have it. my %cnt = ();

### The file is entered on the command line. while (<>) { $cnt{lc chop}++ while length } ### Because I only want alpha and I want them all ... for ('a' .. 'z'){ my @count=(("$_", $cnt{$_}//0));

Thank you for the reply, Laurent. I have worked it out. I will take your advice. I am still not clear when code should be within the curly brackets. I'm sure there is likely a clear rule on this, but so far it is evading me.

You have discovered that your main problem was the location of the brace that ended the for loop. You still do not understand the scope of lexical (my) variables. (Refer: perldoc -f my ) (and its links!).

By far, the main role of the curly braces is to create a block of code that runs usually together and whose execution is determined by some specific condition, such as an "if" conditional or a "while" or a "foreach" loop. You generally want to have all the instructions within the curlies to execute together, depending on whether the condition is true or not.

There are some other reasons to group pieces of code together in a block enclosed within curlies, but this is a slightly more advanced topic, remember for the time being that if you write:

Code

if ($some_condition) { instruction 1; instruction 2; instruction 3; }

this means that you want all three instructions to be executed if $some_condition evaluates to true, and none of them if $some_condition is false. Whatever comes after the closing curly is no longer dependent on $some_condition.