#!/usr/local/bin/perl -w use strict; use Math::Polynomial; my %h = (); my $s = "Just another Perl h"; # put string into a hash {0=>J, 1=>u ...) map { $h{$_}=ord(substr($s,$_,1))}(0..length($s)-1); # use the library to get the polynomial factors my @p = split / /, Math::Polynomial::interpolate(%h); # generate the calculation string from factors my $p; my $i = length($s)-1; map{ $p .= "+$_*\$i**$i";$i-- } @p; # tidy the string, remove $i**0 (=1) # add .5 to total for rounding purposes $p =~ s/\+\-/\-/g; $p =~ s/.*\(//; $p =~ s/\).*/.5/; print "$p\n"; # test the equation for $i (0..length($s)-1) { print chr eval $p; }