Re: [SOLUTION] solution to qotw 2005/03

Luke Triantafyllidis <[email protected]> Thu, 17 Feb 2005 14:23:52 +1000
Newsgroups gmane.comp.lang.perl.qotw.discuss
Message-ID <[email protected]>
Heh, umm sorry for the late entry, but I've been busy as hell. This is a 
fairly hack way around it, but it seems to work although I haven't 
really extensively tested it that much. Oh well, don't expect much for 
15 minutes work.

Luke

-- 
Luke Triantafyllidis
mail:    triple __at__ aeoth.net
web:     http://triple.aeoth.net
03-2005.pl (text/plain, 596 B)
#!/usr/bin/perl -w

use strict;

my @strings = qw(Jonathan Sam Abby Daniel Julia Terrence Constance Al);

print_list($ARGV[0], @strings);

sub print_list
{
	my ($num, @list) = @_;
	my $maxLen = 0;
	for my $idx (@list)
	{
		$maxLen = length $idx if(length $idx > $maxLen);
	}

	$maxLen++;
	my $line = '';

	for my $idx (sort { lc $a cmp lc $b } @list)
	{
		$line .= $idx;
		if(length $line > $num)
		{
			$line =~ s/\s*$idx$//;
			print $line, "\n" if(length $line > 0);
			$line = $idx;
		}
		$line .= ' ' x ($maxLen - length $idx);
	}

	$line =~ s/\s*$//;
	print $line, "\n";
}

# vim:ts=4:sw=4