2008 Winter Scripting Games

Solution to Beginner Perl Event 4: Count Yourself In

Event 4 Solution


Perl solution to Event 4 in the 2008 Winter Scripting Games.

Solutions are also available for VBScript and Windows PowerShell.

*

Event 4 – Count Yourself In

To correctly solve this event you needed to be able to determine the name of the running script and read from that script file. Here’s one possible solution:

$script = $0;
open FH, $script;
@lines = (<FH>);
for my $line (@lines)
{
    chomp $line;
    $a += length($line);
}
print "$a";

This is a pretty straightforward script. The first line is probably the key to this script:

$script = $0;

If you got this line you were well on your way to solving this event. (And if you read the Scripting Games Tips you got this line.) All we’re doing here is assigning the reserved variable $0 to the variable $script. And what is in $0? That just happens to be the full path and filename to the running script – this script, as a matter of fact.

Now we want to open this script file. Is it a problem to open a file while the script is running? Not at all. The script is running in the computer’s memory, and opening the file opens it in another section of memory; the two copies really don’t have much to do with each other at that stage. In order to open the file, we need to call the open method:

open FH, $script;

We pass two parameters to open: a variable (FH) into which serves as the reference to the file; and $script, the path to our script file.

To get at the contents of our open file, we do this:

@lines = (<FH>);

The syntax might look a little strange, but this line reads the entire contents of the file and puts it into the array @lines. Each element in the @lines array contains one line of the script file. That means we can count all the characters in the file by setting up a foreach loop to loop through the array and counting the characters in each line. Here’s our foreach loop:

for my $line (@lines)

Yes, we did just use for rather than foreach – Perl lets you do that, so we did.

We could simply count the characters in the script from here, but if you did that you didn’t get any points for this event. The instructions for this event said that the count should not include the carriage-return linefeed. That means that before we count the characters in the file we need to strip out all the carriage-return linefeed characters. We do that with the chomp function:

chomp $line;

The chomp function simply strips the carriage-return linefeed character from the string.

Now we can count the number of characters that are left in the string:

$a += length($line);

We use the length method to determine the length of the string, passing it the string we want the length of. Each time through the foreach loop we add the value returned by length to the variable $a. If you’re wondering how that’s happening, += is Perl shorthand for “add to itself.” In other words this line:

$a = $a + 1;

is equivalent to this line:

$a += 1;

We’re simply saying “add this value to $a then assign the result back to $a.”

You don’t get any points in this event for simply counting the characters though – you actually have to display the result:

print "$a";

That wasn’t so hard, was it?


Top of pageTop of page