Donums Perl Code

This page contains the Perl that is used to implement the green triangle and blue circle JavaScript code.
#
# Scan through a file and replace each occurrance of 0 with
# VARIABLE`s value. When  or  is encountered,
# increment or decrement the previously scanned variable. Variables are
# initialized to 0. Other operators could be allowed. For example one of the
# operators of the form += could be followed immediately by a variable
# (e.g. 0*=2 would multiply variable by 2). Only 2
# character operators need be allowed (**= is probably not needed).
#
# Usage:
# perl donums.pl pagefile.aki > pagefile.htm
#
#------------------------------------------------------------------------------

require 5.004;

use English;
use strict;

my @fields;
my $n_fields;
my $i;
my $thing;
my $variable;
my %values;
my $value;
my $line;

while(<>) {
 @fields = split(/\|/);
 $n_fields = @fields;
 if ($n_fields >= 5) {
 $line = "";
 for($i=0; $i<$n_fields; $i++) {

 if (($i < $n_fields - 2) &&
 ($fields[$i] eq "variable") &&
 ($fields[$i+2] eq "")) {
 # Found something of the form 0.
 # The value "thing" can be either the name of a variable whose
 # value is to be outputted, or an operator on the last variable
 # that was referred to. Currently only ++ and -- operators are
 # supported.
 $thing = $fields[$i+1];
 $i++;
 if ($thing eq "++") {
 if ($variable ne "") {
 $values{$variable}++;
 }
 }
 elsif ($thing eq "--") {
 if ($variable ne "") {
 $values{$variable}--;
 }
 }
 else {
 $variable = $thing;
 $value = $values{$variable} + 0;
 $line = $line . $value;
 }
 }
 else {
 # No special value was encountered. Concatenate this field to
 # pass this field to the output line.
 $line = $line . $fields[$i];
 }
 }
 print $line;
 }
 else {
 print $_;
 }
}

Back to The Information Cave home page

Last modified Sat Aug 21 21:19:17 1999.