Three word location: Difference between revisions

m
→‎{{header|Perl}}: errant 'join' led to un-needed complexity
(Added Perl example)
m (→‎{{header|Perl}}: errant 'join' led to un-needed complexity)
Line 472:
 
sub thnys {
my @n = @htnys{ ${shift() } =~ /(..)(..)(..)/ }; # NB notation on hash slice: % -> @
($n[2] + $n[1]*$exp + $n[0]*$exp**2) % 28126
}
Line 484:
my $bits = sprintf '%021b%022b', int(($lat+90)*$prec), int(($lon+180)*$prec);
push @words, &$f(bin2dec($_)) for $bits =~ / (.{15}) (.{14}) (.{14}) /x;
join ' ', @words
}
 
sub w_decode {
my($tripletw, $f) = @_;
my @t = split ' ', $triplet->[0];
$f = \&thnys unless $f;
my $s = '%015b';
my $bin = sprintf($s, &$f(\$t$w[0])) . substr(sprintf($s, &$f(\$t$w[1])), 1) . substr(sprintf($s, &$f(\$t$w[2])), 1);
(bin2dec(substr($bin,0,21))/$prec - 90), (bin2dec(substr($bin,21))/$prec - 180)
}
Line 501 ⟶ 500:
[ 21.2596, -157.8117, 'Diamond Head crater'],
[-55.9652, -67.2256, 'Monumento Cabo De Hornos'],
[ 71.170924, 25.782998, 'Nordkapp, Norway'],
[ 45.762983, 4.834520, 'Café Perl, Lyon'],
[ 48.391541, -124.736731, 'Cape Flattery Lighthouse, Tatoosh Island'],
Line 509 ⟶ 508:
my @index = w_encode $lat, $lon, sub { shift };
printf "Coordinates: %s, %s (%s)\n To Index: %s\n To 3-word: %s\nFrom 3-word: %s, %s\n From Index: %s, %s\n\n",
$lat, $lon, $description, join(' ',@index), join(' ',@words), w_decode(\@words), w_decode(\@index, sub { ${shift()} });
}</lang>
{{out}}
Line 530 ⟶ 529:
From Index: -55.9652, -67.2256
 
Coordinates: 71.170924, 25.782998 (Nordkapp, Norway)
To Index: 25182 15741 9829
To 3-word: zorenu jaboda kiyika
2,392

edits