I wanted to make something distinctly perl, and despite its many powers, perl is best known for its unmatched text manipulation capabilities. Therefore, I endevored to write a binary clock program based upon string matching. It operates as a finite state machine, where the current state is the binary string representing each digit. No arithmatic takes place.
I was able to construct the core of the clock in one horrendously (beautiful?) long line of code,with a couple of extra lines for initilization and printing.
The current time can be set using command line aurguments like:
perl 438277.pl 0000 0011 0001 0111
The example above sets the current time to 3:17. The clock defaults to 1:00 if no command line aurguments are provided.
If you really want time to fly, change the '1' in the 'select $,,$,,$,,1;' statement to '0'.use warnings;use strict; (@ARGV eq 4)?(($",$~,$:,$.) = @ARGV,$^=$_='0000'): ($"=$_=$^=$.=$:='0000',$~='0001');{$|=1;select $,,$,,$,,1; ((s:0000:0001:)?():((s:0001:0010:)?():((s:0010:0011:)?():((s:0011:0100 +:)?():((s:0100:0101:)?():((s:0101:0110:)?():((s:0110:0111:)?():((s:01 +11:1000:)?():((s:1000:1001:))))))))))?():((s:1001:0000:)?((($^=~s:000 +0:0001:)?():(($^=~s:0001:0010:)?():(($^=~s:0010:0011:)?():(($^=~s:001 +1:0100:)?():(($^=~s:0100:0101:))))))?():(($^=~s:0101:0000:)?((($.=~s: +0000:0001:)?():(($.=~s:0001:0010:)?():(($.=~s:0010:0011:)?():(($.=~s: +0011:0100:)?():(($.=~s:0100:0101:)?():(($.=~s:0101:0110:)?():(($.=~s: +0110:0111:)?():(($.=~s:0111:1000:)?():(($.=~s:1000:1001:))))))))))?() +:(($.=~s:1001:0000:)?((($:=~s:0000:0001:)?():(($:=~s:0001:0010:)?():( +($:=~s:0010:0011:)?():(($:=~s:0011:0100:)?():(($:=~s:0100:0101:)))))) +?():(($:=~s:0101:0000:)?((($~=~s:0000:0001:)?():(($~=~s:0001:0010:)?( +):(($~=~s:0010:0011:)?(($"=='0001')?($~=$",$"='0000'):()):(($~=~s:001 +1:0100:)?():(($~=~s:0100:0101:)?():(($~=~s:0101:0110:)?():(($~=~s:011 +0:0111:)?():(($~=~s:0111:1000:)?():(($~=~s:1000:1001:))))))))))?():(( +$~=~s:1001:0000:)?(($"=~s:0000:0001:)?():($"=~s:0001:0000:)):())):()) +):())):())):()), (print "\r",$",' ',$~,':',$:,' ',$.,':',$^,' ',$_)&&redo}
UPDATE: Something went wrong when I posted the code the first time. I have replaced it with code that should work as advertized.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Binary Clock Implemented Through String Matching
by belg4mit (Prior) on Mar 12, 2005 at 15:09 UTC | |
|
Re: Binary Clock Implemented Through String Matching
by zentara (Cardinal) on Mar 11, 2005 at 12:17 UTC | |
|
Re: Binary Clock Implemented Through String Matching
by Vynce (Friar) on Mar 16, 2005 at 09:23 UTC | |
by belg4mit (Prior) on Mar 21, 2005 at 07:39 UTC |