Cellular Automaton Knitting with Perl

What is knitting?

Knitting is a means of making fabric by pulling loops of yarn through other loops.
Loops, called 'stitches,' are the discrete unit of knitted fabric. Knitted fabric, worked in rows or spirals, grows in one direction.

What is a cellular automaton?

"A regular spatial lattice of "cells", each of which can have any one of a finite number of states... Each cell in a cellular automaton ... takes its neighbours' states as input and outputs its own state."
http://computing-dictionary.thefreedictionary.com/cellular%20automaton

What do CA have to do with knitting?

In knitting we use rule-based 1-D cellular automata as a design tool. Like knitted fabric, these are composed of discrete units and grow in one direction. The state of each stitch's neighbors (typically its color) determines the state of the stitch.

The rule is like a truth table with three inputs (stitch directly below, stitch below left, stitch below right) and an output. The diagram shows a rule in which three white stitches determine a black stitch, and all other combinations, a white stitch.

Read from left to right, the binary number created by the outputs is 00000001, or 1. Such rules can be summarized as a number from 0 to 255.

So how can Perl help?

Determining the color of each stitch by eye, then knitting it, is SLOW. A short Perl script can generate knitting instructions to speed up the process.
% caknit -s -r -w -c [-f]
-s: number of stitches
-r: number of rows
-w: rule (Wolfram's number)
-c: comma-separated list of two colors
-f: first row (or omit to generate random)
It represents a row as a circular array.
At the beginning of each row, it uses the last stitch of the previous row in its calculation.
At the end, it uses the first stitch of the same row in its calculations.

Handy printout for E-Z knitting:

rows: 20  stitches: 20  wolfram: 105
colors:  lightgrey (L), darkgrey (D)
1D CA rule: 
DDD: D
DDL: L
DLD: L
DLL: D
LDD: L
LDL: D
LLD: D
LLL: L
      
      

  1:  L D D L L  L D D L L  D D L L D  D L D D D  
  2:  L L L D L  D L L D D  L L D D L  L L L D L  
  3:  L L D D L  D D D L L  D D L L D  L L D D D  
  4:  D D L L L  L D L D D  L L D D D  D D L D L  
  5:  L L D L L  D D L L L  D D L D D  D L L D L