x
This website is using cookies. We use cookies to ensure that we give you the best experience on our website. More info. That's Fine
HPC:Factor Logo 
 
Latest Forum Activity

Porting a PHP function to PerlCE

fragenmensch Page Icon Posted 2005-11-28 3:07 PM
#
Avatar image of fragenmensch
Factorite (Elite)

Posts:
207
Location:
DE-DE
Status:
Is it possible to port this function into PerlCE?
function checksum($number)
{
$multipliers ="731";
for($i = 0; $i < strlen($number); $i++
{
$sum += $number{$i} * $multipliers{$i%3};
}
return $sum % 10;
}

Marco

PS: WTF do I need to port PHP4 to CE??
I think this would be a nice job to do...
 Top of the page
chiark Page Icon Posted 2005-11-28 3:13 PM
#
Avatar image of chiark
H/PC Sensei

Posts:
1,330
Location:
North of England
Status:
Yup, of course it's possible - it's a simple checksum routine. It's practically the same in PERL.

Pick up some basics of PERL and you'll see how straightforward that is - the only thing you might not recognise is the % operand, which is the same as MOD in Visual Basic.

I'll leave you to work out the PERL as it will help you. If you get stuck, post what you think it might be and we'll sort it out - that should be more help than just telling you the perl

Good luck.

As for porting php (the language) to ce, er... Forget it. Months if not years of effort, and given that PHP is aimed at web hosting, what would you achieve?
 Top of the page
fragenmensch Page Icon Posted 2005-11-28 4:00 PM
#
Avatar image of fragenmensch
Factorite (Elite)

Posts:
207
Location:
DE-DE
Status:
chiark - 2005-11-28 9:13 PM

Yup, of course it's possible - it's a simple checksum routine. It's practically the same in PERL.

Pick up some basics of PERL and you'll see how straightforward that is - the only thing you might not recognise is the % operand, which is the same as MOD in Visual Basic.

I'll leave you to work out the PERL as it will help you. If you get stuck, post what you think it might be and we'll sort it out - that should be more help than just telling you the perl

Good luck.

As for porting php (the language) to ce, er... Forget it. Months if not years of effort, and given that PHP is aimed at web hosting, what would you achieve?


The same code- in PERL the modulus is % too. Only thing to change is the function declare...
And PHP for handhelds: PHP isn't only good for webservers.I think it would be useful as an "perl for n00bs".

Marco
 Top of the page
chiark Page Icon Posted 2005-11-28 4:06 PM
#
Avatar image of chiark
H/PC Sensei

Posts:
1,330
Location:
North of England
Status:
Sorry, I thought you might not know that % was modulus, as I thought you only knew VB

PHP is designed as a hypertext preprocessor, hence me thinking it's not terribly suitable for a general purpose language. Stick with PERL
 Top of the page
fragenmensch Page Icon Posted 2005-11-28 4:10 PM
#
Avatar image of fragenmensch
Factorite (Elite)

Posts:
207
Location:
DE-DE
Status:
I speak VB,PHP,bits of PERL,HTML, VBScript, PocketC and rude JavaScript;and basics of FLASH.

Marco
 Top of the page
chiark Page Icon Posted 2005-12-05 6:14 AM
#
Avatar image of chiark
H/PC Sensei

Posts:
1,330
Location:
North of England
Status:
OK, more help...

You can't access substrings in PERL like you can in PHP - by treating them as an array - so you need to unpack them into an array. The unpack function does this.

The index brackets (or designators) for PERL standard arrays are [], not {}.

Something like this'll do what you want.

$a=checkSum(101);
print $a;

sub checkSum
{
local $number=shift @_;
local @multipliers =(7,3,1);
local $i;
local $sum;

local @num=unpack("A1" x length($number),$number);

for($i = 0; $i < length($number); $i++)
{
#print "$i... ($num[$i] * $multipliers[($i%3)])";
$sum += $num[$i] * $multipliers[$i%3];
}
return $sum % 10;
}

A bit of explanation:
@_ is the array of parameters passed to the function - "shifting" removes the first element, assigning it to the variable and leaving the array 1 element shorter, starting at element 2.

"local" just tells perl to limit the scope of the variable to the subroutine/function.

unpack takes a string and splits it into a list/array based on the template provided. "A1" is a template string saying "1 character", and the x operator multiplies strings in PERL.

Hope this helps. If you're stuck, just shout.
 Top of the page
fragenmensch Page Icon Posted 2005-12-05 1:56 PM
#
Avatar image of fragenmensch
Factorite (Elite)

Posts:
207
Location:
DE-DE
Status:
Ah, thanks. The only thing I didn't figure out(but I knew it before summer holidays) was the one with the UNPACK function.

Marco
 Top of the page
fragenmensch Page Icon Posted 2005-12-05 3:01 PM
#
Avatar image of fragenmensch
Factorite (Elite)

Posts:
207
Location:
DE-DE
Status:
Sorry for double-post, it was too late for editing...

This is the function I made out of it:
For explain:
$bkz and $sn: Both part of the serial number of the ID card
$birth: Birth date(YYMMTT format)
$valid: How long is the ID card valid(Again YYMMTT)
$perso: The complete number, incl. checksums of the three components and global checksum
$perso_str:$perso in format of real ID card

print "Bitte BKZ eingeben(4 Stellen)\n";
$bkz=<stdin>;
chomp($bkz);
print "Bitte Seriennummer eingeben(5 Stellen)\n";
$sn=<stdin>;
chomp($sn);
$sn_c=$bkz . $sn;
$sn_c .= checkSum($sn_c);
$perso = $sn_c;
$perso_str=$sn_c . "D<<";

print "Bitte Geburtsdatum eingeben(JJMMTT)\n";
$birth=<stdin>;
chomp($birth);
$birth .= checkSum($birth);
$perso .= $birth;
$perso_str .= $birth . "<";

print "Bitte Ablaufdatum eingeben(JJMMTT)\n";
$valid=<stdin>;
chomp($valid);
$valid .= checkSum($valid);
$perso .= $valid;
$perso_str .= $valid . "<<<<<<<";

$perso_str .=checkSum($perso);
$perso .=checkSum($perso);

print "Ihre Personalausweis-Nummer: \n$perso_str \nBitte drücken Sie RETURN";
$dummy=<stdin>;
chomp($dummy);
sub checkSum
{
local $number=shift @_;
local @multipliers =(7,3,1);
local $i;
local $sum;

local @num=unpack("A1" x length($number),$number);

for($i = 0; $i < length($number); $i++
{
#print "$i... ($num[$i] * $multipliers[($i%3)])";
$sum += $num[$i] * $multipliers[$i%3];
}
return $sum % 10;
}

Oh...for what this is useful: You give the program some dates, and it generates vaild ID card numbers vor Germany(AFAIK for other EU countries too).
 Top of the page
Jump to forum:
Seconds to generate: 0.156 - Cached queries : 66 - Executed queries : 8