I can’t help finding this slightly elegant. Unfortunately I no longer remember where the idea came from.

#!/usr/bin/perl
use strict;
use warnings;
sub longest_common_prefix {
    my $prefix = shift;
    for (@_) {
	chop $prefix while (! /^$prefix/);
    }
    return $prefix;
}
print longest_common_prefix(@ARGV), "
";

#

September 9, 2005

Comments

Leave a Reply