Line Filters in ActionScript

A line filter is a common type of program that reads input, processes it, and then prints some derived result. Here’s an example line filter in ActionScript that writes a capitalized version of all input text. You can use this pattern to write your own ActionScript line filters.

package {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.text.TextField;
    import flash.text.TextFieldType;

    public class LineFilter extends Sprite {
        private var input:TextField;
        private var output:TextField;

        public function LineFilter() {
            input = new TextField();
            input.type = TextFieldType.INPUT;
            input.width = 200;
            input.height = 100;
            input.x = 10;
            input.y = 10;
            addChild(input);

            output = new TextField();
            output.width = 200;
            output.height = 100;
            output.x = 10;
            output.y = 120;
            addChild(output);

            input.addEventListener(Event.CHANGE, processInput);
        }

        private function processInput(event:Event):void {
            var lines:Array = input.text.split("\n");
            var upperCaseLines:Array = lines.map(function(line:String):String {
                return line.toUpperCase();
            });
            output.text = upperCaseLines.join("\n");
        }
    }
}

In this ActionScript example, we create a simple user interface with two text fields: one for input and one for output. The processInput function is called whenever the input text changes. It splits the input into lines, converts each line to uppercase, and then joins the lines back together to display in the output field.

To use this line filter:

  1. Create a new ActionScript project in your preferred IDE or text editor.
  2. Copy the above code into a file named LineFilter.as.
  3. Compile and run the project.
  4. Type or paste text into the upper text field.
  5. The lowercase text will be automatically converted to uppercase in the lower text field.

Note that ActionScript, being primarily used for web and Adobe AIR applications, doesn’t have direct access to standard input and output streams like command-line programs do. Instead, this example uses text fields for input and output, which is more common in ActionScript applications.

This example demonstrates how to process text line by line in ActionScript, converting each line to uppercase. You can modify the processInput function to perform different operations on the input text, allowing you to create various types of line filters in ActionScript.