function Automaton(substr) {
this.delta = [{"a":0, "b":1}, {"a":1, "b":0}];
this.state = 0;
this.search = function(str) {
this.state = 0;
for (var i = 0; i < str.length; i++) {
this.state = this.delta[this.state][str.charAt(i)];
WSH.Echo(this.state);
}
}
}
var a = new Automaton("...");
a.search("ababbbba");
[1]
2
my $paddr = inet_aton($addr);
croak "Cannot resolve '$addr'" if (!defined $paddr);
socket SOCK, PF_INET, SOCK_STREAM, getprotobyname('tcp')
or croak 'Cannot create socket';
connect SOCK, sockaddr_in($port, $paddr)
or croak "Cannot connect to server '$server'";
send SOCK, "$query\r\n", 0;
vec($rin, fileno(SOCK), 1) = 1;
do {
($nfound) = select($rout = $rin, '', '', $timeout);
croak 'Select() error' if ($nfound == -1);
croak 'Timeout' if ($nfound == 0);
croak 'Recv() error' if (!defined(recv SOCK, $buf, 512, 0));
$answer .= $buf;
} while $buf ne '';
shutdown SOCK, 2;
sub sendRaw($$$) {
use constant IPPROTO_RAW => 255;
my ($destination, $dport, $packet) = @_;
eval {
socket(SOCKET, PF_INET, SOCK_RAW, IPPROTO_RAW);
my $paddr = sockaddr_in($dport, inet_aton($destination));
send(SOCKET, $packet, 0, $paddr);
};
if ($@) {
print "Error occured: $@";
}
}
sub assembleRawUdp($$$$$) {
my ($destination, $dport, $source, $sport, $data) = @_;
#!/usr/bin/perl
use Tk;
sub Exit {
exit(0);
}
sub showHelp {
$tl = $root->Toplevel;
$tl->title("Help");
$tl->Label(-text => '(c) tswr')->pack();
}
Source
<html>
<head>
<title>Snippet for "Hamming code"</title>
<style>
body { padding: 5px; background-color: lightgoldenrodyellow }
</style>
<script type="text/javascript">
function copy() {
var input = document.getElementById('input');
var output = document.getElementById('output');
output.value = input.value;
}
</script>
</head>
<body>
<h4>Snippet for "Hamming code"</h4>
<input type='text' id='input'></input>
<input type='button' onclick='copy()' value='copy'></input><br>
<input type='text' id='output'></input>
</body>
</html>
Demo
[1]
2