#!/bin/sh prev=0 next=1 echo $prev while(true) do echo $next #add the two numbers sum=$(($prev+$next)) #swap prev=$next next=$sum sleep 1 done
Friday, June 26, 2009
Fibonacci Shell Script
Here is a quick unix shell script which prints out the Fibonacci sequence:
0,1,1,2,3,5,8,13,21,34,55,89,144,...
The first two Fibonacci numbers are 0 and 1, and each remaining number is the sum of the previous two.
Monday, June 01, 2009
Using XPath in Java
Given the following xml document:
<hosts> <host name="starship" port="8080"/> <host name="firefly" port="8180"/> </hosts>this is how you can use the
javax.xml.xpath library to run an XPath query in order to obtain a list of host names:
//create a document
DocumentBuilderFactory domFactory =
DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("file.xml");
//create the xpath expression
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("//host/@name");
//run the xpath query
Object result = expr.evaluate(doc, XPathConstants.NODESET);
//read results
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
Subscribe to:
Comments (Atom)