Perl strict Benchmark

This is a Example-Benchmark about to turn off things you don’t need in Perl:

use Benchmark qw(:all) ;
 
cmpthese(-1, {
'dev'      => sub {my($x,$y) = (4,3); $x = $x/$y;},
'strict'   => sub {my($x,$y) = (4,3); use strict; $x = $x/$y;},
});

Our Result:

            Rate    dev strict 
dev    3841286/s     --    -8%  
strict 4170472/s     9%     --

Maybe a good solution is to use it to check your code and then to comment it out, maybe with a debug variable, for this take a look at Perl Debug Output.

(This is just an Example, don’t turn of use strict.)

Perl random password generator

This is an little example how to generate some password with numbers,lower letter, upper letter and some special chars in Perl :

use strict;

print   randPW(join("",0..9),3).
        randPW(join("",'A'..'Z'),4).
        randPW(join("",'a'..'z'),4).
        randPW("+-*:;!@",3).$/ for(0..10);


sub randPW {
my @arr = glob(("{".join(",",split("",$_[0]))."}")x$_[1]);
return($arr[int rand($#arr)])    
}

Some random passwords:

728UEJXjltw+!+
019PBRPzsgm+-+
266MQSDizmg:!:
327YNEXqsgt:@:
183XTANfktm;-;
812BMXEpbwz::;
108TAFJbcve;+!
288SMLIbqko!!@
393JTJXoktj--:
702IKLHfetm++!
875VQUKmsyy+;:

Perl trim decimal number

This is a function to trim a decimal number in Perl, this function did not round the number just make an sub string from the dot to the end:

use strict;

print trim_deciamal("2.1",3).$/;
print trim_deciamal("2.12",3).$/;
print trim_deciamal("2.123",3).$/;
print trim_deciamal("2.1234",3).$/;
print trim_deciamal("2.12345",3).$/;

sub trim_deciamal{
my ($f,$h) = split(/\./o,$_[0]);
if(length($h) >= $_[1]){
    $h = substr($h,0,$_[1]);    
}else{
    $h = sprintf("%-".$_[1]."s",$h);
    $h =~ tr/ /0/;
}
return($f.".".$h);
}

Result looks like this:

2.100 
2.120 
2.123 
2.123 
2.123

we see missing positions are filled with 0 and longer numbers are trimmed.

Perl sub Benchmark

This is a Benchmark about the subroutines in Perl:

use Benchmark qw(:all) ;
 
sub calc { return($_[0]*$_[1]);}

my %subs;
$subs{'calc1'} = eval 'sub { return($_[0]*$_[1]);}';
$subs{'calc2'} = sub { return($_[0]*$_[1]);};
print $subs{'calc1'}->(1234,1234).$/;
print $subs{'calc2'}->(1234,1234).$/;
print calc(1234,1234).$/;
 
cmpthese(-2, {
'normal'  => sub {calc(1234,1234)},
'comp'    => sub {$subs{'calc1'}->(1234,1234);},
'ref'     => sub {$subs{'calc2'}->(1234,1234);},
});

Result:

1522756
1522756
1522756
            Rate    ref   comp normal
ref    1920352/s     --    -4%   -17%
comp   1996495/s     4%     --   -14% 
normal 2313035/s    20%    16%     --

We see the normal way is the best solution.

Perl random number

This is a bit more complex solution to generate a random number in Perl:

use strict;
sub num {
    my $ret = '';
    while($_[0] =~ /(\d)/g) {
        $ret .= $1;
    }
    return($ret);
}

sub myshuffle {
    my @str = ($#_ == 0)?split(//o, $_[0]):@_;
    my $r1 = 0;
    my $r2 = 0;
    for(0..($#str*2)){
        $r1 = int(rand($#str+1));
        $r2 = int(rand($#str+1))while($r1 == $r2 && $#str>4);
        ($str[$r1],$str[$r2]) = ($str[$r2],$str[$r1]);
    }
    return join '', @str;
}

sub myrand {
    my $c = 0;
    my $s = '';
    my $r1 = 0;
    my $r2 = 0;
    for(1..100){
        $r1 = int(rand(10+$c));
        $r2 = int(rand(10+$c))while($r1 == $r2);
        $s = crypt(myshuffle($r2.$s.$c.$r1),int($c+rand(99999)));
        $c = num($s);
    }
   return($c); 
}


print myrand().$/;

It has no max value but normal the highest is 99999.

 

 

Perl bignum Benchmark

This is a Benchmark in Perl about bignum :

use Benchmark qw(:all) ;

cmpthese(-1, {
'dev'      => sub {my($x,$y) = (4,3); $x = $x/$y;},
'bignum'   => sub {my($x,$y) = (4,3); use bignum; $x = $x/$y;},
});

Our result:

            Rate    dev bignum 
dev    4670926/s     --   -11% 
bignum 5224717/s    12%     --

We see the cost is about 12% performance, so if its not really necessary we should tun this feature off.

Perl default defined variables – store and restore

This is a little Script to store all default defined variables from Perl(Linux) into an Scalar ant then to restore everything like it was before the store function.

{
my @_STORE_Course = ();         #scalar for Regex and program course
my @_STORE_Environment = ();    #scalar for environment
	#store sub
	sub StoreDefVars(){
		@_STORE_Course = ($.,$/,$\,$,,$",$|,$%,$=,$-,$~,$^,$:,$^L,$^A);
		@_STORE_Environment = ($^W,$^X);
	}
	#load sub
	sub LoadDefVars(){
		($.,$/,$\,$,,$",$|,$%,$=,$-,$~,$^,$:,$^L,$^A) = @_STORE_Course;
		($^W,$^X) = @_STORE_Environment;
	}
	sub ChangedDefVars(){
	    my @_CACHE = ($.,$/,$\,$,,$",$|,$%,$=,$-,$~,$^,$:,$^L,$^A);
	    for(0..$#_STORE_Course){
	        return(1) if($_STORE_Course[$_] ne $_CACHE[$_]);
	    }
	    
	    @_CACHE = ($^W,$^X);
	    for(0..$#_STORE_Environment){
	        return(1) if($_STORE_Environment[$_] ne $_CACHE[$_]);
	    }
	    return(0);
	}
	sub PrintDevVars(){
	    print dumper(\@_STORE_Course),"\n";
	    print dumper(\@_STORE_Environment),"\n";
	}
}

Important to include my Data-Dumper !

If you want to store your data in the intern arrays, run this:

StoreDefVars();
  • scalar for Regex and program course are stored in “@_Course”
  • scalar for environment are stored in “@_Environment”
  • hashes and arrays are not stored because they are global or lower scope

only variables that writeable are getting stored.

Then restore your saved variables:

LoadDefVars();

you could see if you changed any default variable with(1=changed):

ChangedDefVars()

Or you could print all Stored Variables:

PrintDevVars

(Important to include my dumper.)

 


scalar for Regex and program course:

$_ – contains current value of a loop

for("one","two","three"){
    print $_." ";
}
one two three

$& – contains match of the current Regex
$` – contains the left side of the current Regex
$’– contains the right side of the current Regex

my $var = "one two three";
$var =~ /two/;
print $`,"-",$&,"-",$',"\n";
one -two- three

$1,$2,.. – contains matching groups of Regex
$+ – contains the last group match of an Regex

my $var = "1 2 3 4 5 6 7 8 9";
$var =~ /(\d) (\d) (\d) (\d) (\d) (\d) (\d) (\d) (\d)/;
print $1,"-",$2,"-",$3,"-",$+,"\n";
1-2-3-9

$. – contains the current line number of the input file

open(DATEI, "<test.txt");
while(<DATEI>)
 { print $.," -> ",$_; }
close(DATEI);
1 -> first line
2 -> second line
3 -> last line

$/ – sets the input record separator

print "test".$/;
$/ = ";\n";
print "test".$/;
test
test;

$\ – sets the output record separator, added to every print.

print "test\n";
$\ = ";\n";
print "test";
test  
test;

$, – sets the output field separator, added to ever coma separated element

print 1,2,3,4,"\n";
$, = "-";
print 1,2,3,4,"\n";
1234      
1-2-3-4-

$” – sets the output list separator, added to ever coma separated list

my @arr = (1,2,3,4);
print "@arr\n";
$" = "-";
print "@arr\n";
1 2 3 4   
1-2-3-4

$| – output auto flush, disables the output buffer. 0=off, 1=on

 


Format:

$% – current page number
$= – number of lines per page
$- – number of lines left on the page
$~ – name of the current format
$^ – name of the current head format
$: – delimiter of multiple lines
$^L – delimiter of multiple pages
$^A – accumulator

 


scalar for environment are stored:
$? – contains the last status of a pipe or child process

system($test) == 0
  or die "on system: $?"
on system: 65280

$! – contains the error status of a pipe or child process.

open(DATEI, "<file.txt")
  or die "error: $!";
error: No such file or directory at main.pl line 3.

$@ – contains an empty string if eval works, and if not it contains the error

eval { test("dir"); };
print "eval error:$@\n" if($@);
eval&nbsp;error:Undefined&nbsp;subroutine&nbsp;&main::test&nbsp;called&nbsp;at&nbsp;main.pl&nbsp;line&nbsp;3.

$$ – process id
$< – real user id
$> – effective user id
$( – real group id
$) – effective group id

print "Process ID:$$ \n" ;
print "from | real | effective \n" ;
print "user | $<   | $> \n" ;
print "group| $(   | $) \n" ;
Process ID:23024 
from | real | effective 
user | 20104   | 20104 
group| 20104 20104   | 20104 20104

$0 – current program name

print "I am :$0\n" ;
I am :main.pl

$] – current Perl version(is deprecated use $^V instead)

print "Perl Version $] \n" ;
Perl Version 5.018004

$^D – current debugging flag, run your program wit the -D parameter
$^F – highest file descriptor
$^H – contains the number of syntax checks from use strict and other checks
$^I – is defined or undefined if the Inplace flag is set
$^M – could save data if no more ram is available
$^O -contains the current operating system

print "OS: $^O \n" ;
OS: linux

$^P – contains the current debugging status
$^S – the status of the per interpreter, if eval is running its undefined
$^T – contains the start time of the script

sleep(4);
print "Time:".(time()-$^T)."s".$/;
Time:4s

$^V -current Perl interpreter version

print "Perl Version $^V \n" ;
Perl Version v5.18.4

$^W – contains warning from the -w parameter, 1 if set, else 0
$^X – contains the current Perl interpreter path

print "Perl interpreter: $^X \n" ;
Perl interpreter: /usr/bin/perl

 


hashes and arrays:
@_ – contains the parameter from calling a function

test(1,2,3,4,5);
sub test{
    for(@_){ print $_.", ";}
}
1, 2, 3, 4, 5,

@ARGV – contains the parameter from calling your Perl script

for(@ARGV){ print $_.", ";}
$ perl main.pl one twoe three
one, twoe, three,

@INC -paths where Perl looks for Modules

for(@INC){ print $_.", ";}
/usr/local/lib64/perl5, /usr/local/share/perl5, /usr/lib64/perl5/vendor_perl, /usr/share/perl5/vendor_perl, /usr/lib64/perl5, /usr/share/perl5, .,

@ISA – contains a list of basic classes for object oriented
@EXPORT – Used for Modules to export
@EXPORT_OK – Used for Modules to export, but optional
%ENV – contains environment variables from your system

for(keys %ENV){ print $_.", ";}
LD_LIBRARY_PATH, SMLROOT, GNUSTEP_USER_ROOT, GUILE_LOAD_PATH, GNUSTEP_HOST_CPU, SCRIBACONF, TERM, _, INFOPATH, GNUSTEP_SYSTEM_ROOT, GNUSTEP_HOST, GNUSTEP_FLATTENED, GNUSTEP_MAKEFILES, GNUSTEP_HOST_VENDOR, HOSTNAME, GNUSTEP_HOST_OS, HAXE_STD_PATH, SHLVL, LIBRARY_COMBO, DART_SDK, GNUSTEP_PATHLIST, GNUSTEP_LOCAL_ROOT, PWD, GNUSTEP_NETWORK_ROOT, CLASSPATH, HOME, PATH, GNUSTEP_IS_FLATTENED,

%SIG – contains Signal Handler to communicate to other processes

for(keys %SIG){ print $_.", ";}
PIPE, RTMIN, PWR, NUM32, IOT, NUM57, NUM45, STKFLT, NUM53, NUM62, CHLD, NUM36, KILL, NUM37, RTMAX, STOP, NUM39, NUM61, POLL, NUM38, NUM41, NUM47, XFSZ, URG, NUM50, NUM33, TSTP, TTOU, NUM52, PROF, INT, CONT, UNUSED, NUM56, XCPU, NUM55, CLD, FPE, NUM63, TERM, NUM48, NUM42, ABRT, USR1, TTIN, NUM35, ILL, NUM49, NUM43, NUM58, USR2, TRAP, NUM60, NUM51, BUS, SYS, NUM40, HUP, WINCH, IO, ALRM, NUM59, NUM44, NUM46, SEGV, QUIT, VTALRM, NUM54,

 

If you want a list as comment block use this:

=INFO
## programm course
$_ – contains current value of a loop
$& – contains match of the current Regex
$` – contains the left side of the current Regex
$' – contains the right side of the current Regex
$+ – contains the last group match of an Regex
$. – contains the current line number of the input file
$/ – sets the input record separator
$\ – sets the output record separator, added to every print.
$, – sets the output field separator, added to ever coma separated element
$” – sets the output list separator, added to ever coma separated list
$| – output auto flush, disables the output buffer. 0=off, 1=on
## Format
$% – current page number
$= – number of lines per page
$- – number of lines left on the page
$~ – name of the current format
$^ – name of the current head format
$: – delimiter of multiple lines
$^L – delimiter of multiple pages
$^A – accumulator
## Enviroment
$? – contains the last status of a pipe or child process
$! – contains the error status of a pipe or child process.
$@ – contains an empty string if eval works, and if not it contains the error
$$ – process id
$< – real user id
$> – effective user id
$( – real group id
$) – effective group id
$0 – current program name
$^V – current Perl version
$^D – current debugging flag, run your program wit the -D parameter
$^F – highest file descriptor
$^H – contains the number of syntax checks from use strict and other checks
$^I – is defined or undefined if the Inplace flag is set
$^M – could save data if no more ram is available
$^O -contains the current operating system
$^P – contains the current debugging status
$^S – the status of the per interpreter, if eval is running its undefined
$^T – contains the start time of the script
$^W – contains warning from the -w parameter, 1 if set, else 0
$^X – contains the current Perl interpreter path
=cut

Perl Benchmark IN sub with grep

If you miss the IN function in Perl like the function in PL/SQL, then you could use grep and an array to check this:

grep {$var eq $_} qw(2 3 5 7 11 13 17 19)

I made a Benchmark to check how fast his solution is:

use strict;
use Benchmark qw(:all) ;

my @data = qw(2 3 5 7 11 13 17 19);
cmpthese(-2, {
'grep0'  => sub {
        for my $var (1..20){
            if(grep {$var == $_} qw(2 3 5 7 11 13 17 19)){
               # print "$var is prime!".$/;
            }else{
               # print "$var is not prime!".$/;
            }
        }
    },
'grep1'  => sub {
        for my $var (1..20){
            if(grep {$var == $_} @data){
               # print "$var is prime!".$/;
            }else{
               # print "$var is not prime!".$/;
            }
        }
    },
'or'   => sub {
        for my $var (1..20){
            if($var==2||$var==3||$var==5||$var==7||$var==11||$var==13||$var==17||$var==19){
               # print "$var is prime!".$/;
            }else{
               # print "$var is not prime!".$/;
            }
        }
    },
});

Result:

          Rate grep0 grep1    or  
grep0  74471/s    --   -7%  -61%  
grep1  79714/s    7%    --  -58%  
or    189402/s  154%  138%    --

We see the normal way is the fastest and you could use an defined array to speed up by 7%. The Code is short but much slower than an or comparison.

perl shuffle String and Array

I wrote a little script in Perl to shuffle Arrays or strings:

sub myshuffle {
    my @str = ($#_ == 0)?split(//o, $_[0]):@_;
    my $r1 = 0;
    my $r2 = 0;
    
    for(0..($#str*2)){
        $r1 = int(rand($#str+1));
        $r2 = int(rand($#str+1))while($r1 == $r2 && $#str>4);
        ($str[$r1],$str[$r2]) = ($str[$r2],$str[$r1]);
    }
    return join '', @str;
}

This are my examples:

print myshuffle("abcdefghijklmonopqrs").$/;
print myshuffle(1,2,3,4).$/;
print myshuffle(1,2,3).$/;
print myshuffle(1,2).$/;

and we got his as result:

obrdekohnlicgsfqapjm  
2431 
231 
21

Perl serial key generator

The Problem is that I need a god solution to make a serial key in Perl, and so I wrote this program.You could give him a ID to generate a unique serial or you could just use the random function.

serial

is the function for the serial, it generates 10 Blocks of 8 numbers, the first is the ID, with the ID you could generate the serial, then from 2-9 is the serial and the last block is a quick checksum.

quick_keycheck

It calculates a simple checksum from block 2-9 and number 10. the checksum.Its just a simple calculation to implement it in your client software so they don’t could reverse engineer your serial generator code.

full_keycheck

This makes the full key check you should use this script only in your server software.

 

print "$_ -> ",serial($_)." - ".quick_keycheck(serial($_))." - ".full_keycheck(serial($_)).$/ for(0..20);


sub serial {
    my $k1 = $_[0]//int(rand(999999));
    my $k2 = 0;
    my $k3 = 0;
    my $k4 = 0;
    my $k5 = 0;
    my $k6 = 0;
    my $k7 = 0;
    my $k8 = 0;
    my $k9 = 0;
    my $quicksum = "";
    my $cache = $k1;

    #calculate
    $k2 = digit_sum($cache);
    $cache *= $cache*$k2;
    $k3 = digit_sum($cache);
    $k2 = substr( int($k1*2+$k2*17), 0, 6 );
    $k3 = substr( int($k1*2+$k2*13), 0, 6 );
    $k4 = substr( int($k1*2+$k2*11+$k3), 0, 6 );
    $k5 = substr( int($k1*3+$k2*7+$k3*$k4), 0, 6 );
    $k6 = substr( int($k1*4+$k2*5+$k3*$k4+$k5), 0, 6 );
    $k7 = substr( int($k1*5+$k2*3+$k3*$k4+$k5*$k6), 0, 6 );
    $k8 = substr( int($k1*6+$k2*2+$k3*$k4+$k5*$k6+$k7), 0, 6 );
    $k9 = substr( int($k1*6+$k2*2+$k3*$k4+$k5*$k6+$k7+$k9), 0, 6 );
    
    #shuffle
    for(1..3){
    $cache = $k2+$k3+$k4+$k5+$k6+$k7+$k8+$k9+($k1)x!!!($cache%2);
    $k2 = substr( int($k3*$k8+$k4*$k5+$k6*$k7+($k2%2)+$cache), 1, 7 );
    $k3 = substr( int($k4*$k7+$k5*$k6+$k7*$k8+($k3%3)+$cache), 1, 7 );
    $k4 = substr( int($k5*$k6+$k6*$k7+$k8*$k2+($k4%5)+$cache), 1, 7 );
    $k5 = substr( int($k6*$k5+$k7*$k8+$k2*$k3+($k5%7)+$cache), 1, 7 );
    $k6 = substr( int($k7*$k4+$k8*$k2+$k3*$k4+($k6%11)+$cache), 1, 7 );
    $k7 = substr( int($k8*$k3+$k2*$k3+$k4*$k5+($k7%13)+$cache), 1, 7 );
    $k8 = substr( int($k2*$k2+$k3*$k4+$k5*$k6+($k8%17)+$cache), 1, 7 );
    $k9 = substr( int($k1+$cache+digit_sum($k9)+digit_sum($cache)*digit_sum($1)), 0, 6 );
    }

   #create quicksum
   $quicksum = quickcheck($k2,$k3,$k4,$k5,$k6,$k7,$k8,$k9);
   #return
   return(sprintf('%06s-%06s-%06s-%06s-%06s-%06s-%06s-%06s-%06s-%06s',$k1,$k2,$k3,$k4,$k5,$k6,$k7,$k8,$k9,$quicksum)); 
}

sub digit_sum {
    my $out = 0;
    while($_[0] =~ /(.)/g) {$out += $1;}
    return($out);
}

sub quick_keycheck {
my ($k1,$k2,$k3,$k4,$k5,$k6,$k7,$k8,$k9,$quicksum) = split("-",$_[0]);
return(quickcheck($k2,$k3,$k4,$k5,$k6,$k7,$k8,$k9) eq $quicksum);
}

sub full_keycheck {
my ($k1,$k2,$k3,$k4,$k5,$k6,$k7,$k8,$k9,$quicksum) = split("-",$_[0]);
if($k1==0){return(0)}
return(serial($k1) eq $_[0]);
}

sub quickcheck {
	my ($k2,$k3,$k4,$k5,$k6,$k7,$k8,$k9) = @_;
	my $out = 0;
	$out += digit_sum($_) for(@_);
	for(1..3){
	$out += digit_sum($out)+substr(int($k2+$k3+$k4+$k5+$k6+$k7+$k8+$k9), 0, 8 );
	}
	if($out==0){return(0)}
	return(sprintf('%06s',substr($out, 1, 7)));
}

Serials from 1 to 20 :

0 -> 000000-000000-000000-000000-000000-000000-000000-000000-000000-000000 -  - 0                                                                                                                                                              
1 -> 000001-3998721-7745224-0599396-2052866-2216425-6579748-5182280-312901-6063028 - 1 - 1                                                                                                                                                     
2 -> 000002-9388879-1124434-5415065-3373564-8276835-2940278-2216239-295806-9093630 - 1 - 1                                                                                                                                                     
3 -> 000003-8136824-2769572-3221522-0237729-3271546-0220234-5907911-286637-2156204 - 1 - 1                                                                                                                                                     
4 -> 000004-3076279-6603463-8952898-0437658-4536704-5993699-0569181-331545-1504608 - 1 - 1                                                                                                                                                     
5 -> 000005-1975766-4030413-2291740-0469716-2407084-8577044-4270996-402439-3275906 - 1 - 1                                                                                                                                                     
6 -> 000006-6013437-5064470-1421661-0737926-5095564-5504051-7121567-341836-3901797 - 1 - 1                                                                                                                                                     
7 -> 000007-4968284-8541557-2572685-7292731-6322198-1697878-2764690-199017-0307747 - 1 - 1                                                                                                                                                     
8 -> 000008-0771673-4039673-2219286-1344400-6884137-0254807-8815735-326078-3967676 - 1 - 1                                                                                                                                                     
9 -> 000009-1784857-2796964-4190696-3833352-3627062-7471493-8810778-326826-8526432 - 1 - 1                                                                                                                                                     
10 -> 000010-8454487-5887503-7485665-4249980-7497945-3339953-4741639-564253-2666463 - 1 - 1                                                                                                                                                    
11 -> 000011-3822996-4816144-1076432-6210880-6838638-1214082-2273539-295931-9646262 - 1 - 1                                                                                                                                                    
12 -> 000012-3105051-5182692-1252777-0574682-3206422-2380572-7976814-420609-2299157 - 1 - 1                                                                                                                                                    
13 -> 000013-2497334-3623028-3811689-6380523-4720311-1482726-0164622-368791-9147360 - 1 - 1                                                                                                                                                    
14 -> 000014-0258939-4971510-9606881-8702706-4291176-3432311-5172619-222485-0997619 - 1 - 1                                                                                                                                                    
15 -> 000015-3144206-6477344-1667938-9924078-1751310-3248974-8070005-289566-0372059 - 1 - 1                                                                                                                                                    
16 -> 000016-7893562-1201635-3529485-2735528-9729948-2572572-3166053-346847-3527229 - 1 - 1                                                                                                                                                    
17 -> 000017-3685743-3103374-4084623-5118118-0459651-0630184-8613410-483937-8537423 - 1 - 1                                                                                                                                                    
18 -> 000018-2601883-2383892-7972378-8424561-1093898-9865097-4990720-264832-1279216 - 1 - 1                                                                                                                                                    
19 -> 000019-1294422-8138122-9758807-3694786-1036395-0535036-4923173-259514-8921080 - 1 - 1                                                                                                                                                    
20 -> 000020-3731936-0417432-5018774-1448438-3551307-5267837-1166230-395351-2992236 - 1 - 1

 

This is a little test to make the client software in JavaScript:

<!DOCTYPE html>
<html>

<body>
<script>
function myFunction() {
if(document.getElementById("key").value != ""){
 var res = document.getElementById("key").value.split("-"); 
 var out = 0;
 for(var i = 1;i <= 8;++i){
  out += 1*digit_sum(res[i]);
 }
console.log(out);

 for(var i = 1;i <= 3;++i){
  out += 
1*digit_sum(out)+
1*(((1*res[1]+1*res[2]+1*res[3]+1*res[4]+1*res[5]+1*res[6]+1*res[7]+1*res[8])+"").substring(0,8));
console.log(out);
 }
 
 out = (out+"").substring(1,8)
 out -= res[9];

 if(out == res[9]){
  document.getElementById("result").innerHTML = "Key okay!";
  return(0);
 }
 document.getElementById("result").innerHTML =out ;// "Key okay!"
}else{
 document.getElementById("result").innerHTML = "Key wrong!"
}
}
function digit_sum (number){
var out = 0;
for (var i = 0; i < number.length; i++) {
out += 1*number[i];
}
return(out);
}
</script>
<body>
<input type="text"id="key" rows="1" cols="50" onchange="myFunction()" onkeyup="myFunction()" placeholder="Your Regex here...">
<h5 id="result"></h5>

</body>

</html>

but it only haves a different between 0 and 200 …. so its more try and error.