metafy home > anthracite docs > examples > conditional output


conditional output in anthracite using unix+perl


Note: A "Conditional" Object is in the works for a future version of Anthracite that will go beyond the basic capabilities described here. However, this example should also help solve a number of cases reported by customers where conditional output is required based on the input. Please do not hesitate to contact us if you need any support implementing this solution, we have a sample file of this example ready to e-mail to you upon request.


The Problem:
Anthracite does not currently have an object that allows for a branch in processing depending on the input, also known as "conditional" execution. For example, if you want to output the source data if it contains a hyperlink, but indicate if no links were found otherwise.

Proposed Solution:
Use Anthracite's "UNIX Command" Processor Object in conjunction with the Perl programming language and Perl's command-line executable mode to work around this limitation.

How To:
To change the output depending on the input, we're going to use a very short Perl script that tests the incoming value and either passes it along untouched if it contains a hyperlink, or output the exclamation "no anchor!" if one is not found.

Although it certainly helps to know Perl here, you'll be able to use this recipe no matter what, and if you know any programming language (such as AppleScript), then this should quickly make sense. Here is the sample code for the 10 line Perl script that accomplishes this task, including comments:
	
	# part one: collect the input
	while( <STDIN> ) { 
		$x .= $_;
	}
	
	# part two: test for a link and print the result
	if( $x =~ /a href/i ) {
		print( $x );
	} else {
		print( "no anchor!" );
	}
	

This simple script does two things, first, it collects all the input being sent to the processor into a variable, $x. This means that no matter how many lines are sent in, we're planning on finding a hyperlink anywhere in that input.

Then, it tests that $x variable to see if the characters "a href" appear in it, in either upper- or lowercase letters. If so, we print the entire $x variable, and if not, we print "no anchor!". See, pretty simple.

So simple, in fact, that we're going to collapse the entire script into one line and insert it as an argument in the Anthracite UNIX Command Processor Object.

We've opened up the Anthracite Edit Sheet pretty wide here so you can see the script all on one line as the second argument to the Perl command (the "-e" argument tells Perl to execute the next text as a command):



Not too bad for Perl which has a reputation for being easy to obfuscate.

So, with this script snippet, we can take take this sample input:



And it will be converted into this output:




Of course, your Anthracite processes are likely to be much more complex, but hopefully this sample provides you with another building block that you can use in your data processing system designs.

Here's the simple Anthracite process chain that uses this UNIX Command Processor Object Perl conditional script to produce the example output, this is one of the upcoming sample files with the next release:





This sample file will be included starting with Anthracite version 1.0.9 and is available upon request today.


[ Anthracite Examples ] [ Purchase Anthracite ] [ Metafy Home ]


Last Update: 10/21/2004


Copyright © 2004, All Rights Reserved.