Tuesday, April 18, 2006

Perl: Comparisons are odious

Here's a little bug that I discovered in a Perl script today. It involves a confusion between string comparison operator cmp, and the numerical comparison operator <=>. Here's a sample code which demonstrates the problem:


$FRUIT{0} = [1, 'apple'];
$FRUIT{1} = [2, 'banana'];
$FRUIT{2} = [10, 'pear'];

foreach $id (sort { $FRUIT{$a}->[0] <=> $FRUIT{$b}->[0] } keys %FRUIT)
{
   ( $id, $name ) = @{$FRUIT{$id}};
   print $id . " [" . $name . "]\n";
}
print "===\n";
foreach $id (sort { $FRUIT{$a}->[0] cmp $FRUIT{$b}->[0] } keys %FRUIT)
{
   ( $id, $name ) = @{$FRUIT{$id}};
   print $id . " [" . $name . "]\n";
}


The output looks like this:

1 [apple]
2 [banana]
10 [pear]
===
1 [apple]
10 [pear]
2 [banana]


The operator cmp is a string comparator. It sorts the numbers "1", "2", etc. differently than you might expect. "10" is less than "2" when doing string comparisons.

This is the kind of bug you face with loosely typed languages like Perl.

Monday, April 10, 2006

Java - how to turn off Apache Commons Logging Part II

Last week I discovered how to turn off logging via a directive. Today, I needed to turn off logging in an applet that had been built using those same libraries. It was quick and easy. I create a file called commons-logging.properties, with a single line that looks like this:

org.apache.commons.logging.Log=org.apache.commons.logging.impl.NoOpLog

I then placed that file in the root of my jar file, like this:

jar cvf MyApplet.jar commons-logging.properties com/wereldmuis/MyApplet.class

(Obviously, the properties file has to be sitting in the directory where you run the jar command.)

When I deployed the jar file, comments had been turned off; worked immediately. How pleasant!

There's documentation at Jakarata, but it's not great IMHO. They don't give enough examples.

Saturday, April 08, 2006

Java FileDialog.setFilenameFilter does not work in Windows

FileDialog.setFilenameFilter javadoc

"Filename filters do not function in Sun's reference implementation for Microsoft Windows." Isn't it awful? Apparently this was recognized way back in 1997, and it was decided to do nothing about it. That is, MS said it was Sun's problem, and Sun said it was Microsoft's problem. The bug report thread has a few workarounds. Specifically, dlgFile.setFile("*.txt; *.rtf"); worked for me. But it's ugly and unnatural. *Sigh*...

Thursday, April 06, 2006

I'm experimenting with MusicCubes. Very cute!

http://radio1musicubes.co.uk/view_cube.shtml?id=12157

Wednesday, April 05, 2006

OpenLaszlo - how to sort a list

It is possible to sort a list in OpenLaszlo if the list is created from a datapath. (I haven't tried in any other case, but why create a list without a datapath??).

Here's the code:

<canvas debug="true">
<dataset name="myData">
<myXML>
<person name="Alice"/>
<person name="Mary"/>
<person name="Bob"/>
<person name="Susan"/>
</myXML>
</dataset>

<simplelayout axis="y"/>
<list name="myList">
<textlistitem name="item" datapath="myData:/myXML[1]/person[1]/@show"/>
</list>

<button text="descending" y="70">
<method event="onclick">
myList.item.datapath.setOrder("@name", "descending");
</method>
</button>
<button text="ascending" y="70">
<method event="onclick">
myList.item.datapath.setOrder("@name", "ascending");
</method>
</button>

<button text="fill" y="100">
<method event="onclick">
myList.item.setDatapath("myData:/myXML[1]/person/@name");
</method>
</button>
</canvas>

Notice that the original data is not sorted alphabetically. When you hit the "fill" button, the list is filled with the data. Then you can use the sort buttons to sort the data up and down. Sorting is done on the datapath, not on the list itself. Neat! (I'm using lps-3.1.1 by the way, and this code example runs dandy).

Monday, April 03, 2006

Java - how to turn off Apache Commons Logging

Yippie, I'm working on a Java project at the moment!

I'm using a library that has lots of logging information using the Apache Commons Logging library (commons-logging-api.jar). I'm using JBuilder 2005 for this project, and I could see this huge stream of logging "info" being printed out in the messages window. I needed to turn this off so that I could better see my own output (which I'm currently doing using System.out.println). Here's how to do that:
java ... -Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.NoOpLog MyClass
The dash should be directly in front of the D: -Dorg...

In my case, the command actually went:
C:\Borland\JBuilder2005\jdk1.4\bin\javaw -classpath "blah.jar" -Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.NoOpLog MyClass

but you get the idea.

To get JBuilder to run this command, you go to Run -> Configurations and Edit the configuration you're using. In the Edit panel, under "VM parameters" type -Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.NoOpLog. Messages all gone!