Table of Contents

A Simple Sort

If I was reading this I'd be wondering about sorting. Wonder no more, and behold:

foreach (sort keys %countries) {
        print "The key $_ contains $countries{$_}\n";

Spot the difference. Yes, sort crept in there. If you want the list sorted backwards, some cunning is called for. This is suitably foxy:

foreach (reverse sort keys %countries) {
        print "The key $_ contains $countries{$_}\n";

Perl is just so difficult at times, don't you think ? This works because:

This is a quick example to make sure the meaning of reverse is clear:

print "Enter string to be reversed: ";

@letters=split //,$input;	# splits on the 'nothings' in between each character of $input

print join ":", @letters;	# joins all elements of @letters with \n, prints it
print reverse   @letters;	# prints all of @letters, but sdrawkcab )-:

Perl's list operators can just feed directly to each other, saving many lines of code but also decreasing readability to those that aren't Perl-literate:

print "Enter string to be reversed: ";
print join ":",reverse split //,$_=<STDIN>;

This section is about sorting, so enough of reverse . Time to go forwards instead.