CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
modulo in PERl vs PHP

 



Demerzel
Novice

Dec 20, 2012, 6:44 AM

Post #1 of 9 (2106 views)
modulo in PERl vs PHP Can't Post

I have two pices of code in PERL:

Code
		my $cc = '2521';	 
my $format = '%0'.(24 - $acclen) .'d';
print "format:".$format."\n";
$account .= sprintf($format, $row->{'id'});
my $numer=$account.$cc.'00';
print " ".$numer."=";
print "modulo:".($numer% 97);die();
$account= sprintf('%02d', 98-($numer% 97)).$account;
print "konto:".$account;

and PHP:

Code
		$cc = '2521';	// Kod kraju - Polska 
$format = '%0'.(24 - $acclen) .'d';
print $format;
$account .= sprintf($format, $id);
print " ".$account.$cc.'00'."=".bcmod($account.$cc.'00', 97);
return sprintf('%02d', 98-bcmod($account.$cc.'00', 97)).$account;

$account.$cc. and $account.$cc.'00' are equal but ($numer% 97) and bcmod($account.$cc.'00', 97) are different. Any idea why?


FishMonger
Veteran / Moderator

Dec 20, 2012, 7:55 AM

Post #2 of 9 (2103 views)
Re: [Demerzel] modulo in PERl vs PHP [In reply to] Can't Post

The simple answer is that the bcmod() function does not use the exact same method to calculate the modulus as Perl's modulus operator.

I have not looked at the related C source code of either language, but obviously they need to preform division which returns a floating point number which then is either truncated or rounded to an integer. If it is rounded, is it being rounded up or down (POSIX ceil or floor)?

Why are you stringifying the numbers and unnecessarily using concatenation? PHP might need to use that inefficiency, but Perl doesn't.


(This post was edited by FishMonger on Dec 20, 2012, 7:59 AM)


Demerzel
Novice

Dec 20, 2012, 8:51 AM

Post #3 of 9 (2097 views)
Re: [FishMonger] modulo in PERl vs PHP [In reply to] Can't Post

PHP code is not my. In fact I cant change it. Question is how to get the same result in perl. I could start php script from perl and get result back. But I would like to do it without another script.


FishMonger
Veteran / Moderator

Dec 20, 2012, 9:00 AM

Post #4 of 9 (2094 views)
Re: [Demerzel] modulo in PERl vs PHP [In reply to] Can't Post

At the moment I don't have a system with php to test the differences, so what value does the bcmod function return and how does that differ from what you're getting from Perl? +1 or -1


(This post was edited by FishMonger on Dec 20, 2012, 9:01 AM)


Demerzel
Novice

Dec 21, 2012, 3:46 AM

Post #5 of 9 (2083 views)
Re: [FishMonger] modulo in PERl vs PHP [In reply to] Can't Post

Pointless!

Tried different approach

Code
my $account = system("php /var/www/lms/inetgroup.sdom/branches/scripts/account.php $row->{'id'} $row->{'account'}");

Inside php resut is fine but I cant pass value back to perl.


FishMonger
Veteran / Moderator

Dec 21, 2012, 6:07 AM

Post #6 of 9 (2077 views)
Re: [Demerzel] modulo in PERl vs PHP [In reply to] Can't Post

The system function returns the exit code of the program, not its output.

You need to use the `` backticks or qx() operator. You can learn about them in perldoc perlop


Code
my $account = `php /var/www/lms/inetgroup.sdom/branches/scripts/account.php $row->{'id'} $row->{'account'}`;



Code
my $account = qx("php /var/www/lms/inetgroup.sdom/branches/scripts/account.php $row->{'id'} $row->{'account'}");



Demerzel
Novice

Dec 21, 2012, 6:38 AM

Post #7 of 9 (2071 views)
Re: [FishMonger] modulo in PERl vs PHP [In reply to] Can't Post

By now I should be used to this.
You play smart but in the end it is alweys to hard to give straight answer. Waist of time with you guys.


FishMonger
Veteran / Moderator

Dec 21, 2012, 6:43 AM

Post #8 of 9 (2069 views)
Re: [Demerzel] modulo in PERl vs PHP [In reply to] Can't Post

What are you talking about? I gave you straight answers.

You are the one that left out key info that would have helped in providing the answer you were looking for.


Laurent_R
Veteran / Moderator

Dec 21, 2012, 2:36 PM

Post #9 of 9 (2052 views)
Re: [Demerzel] modulo in PERl vs PHP [In reply to] Can't Post


In Reply To
By now I should be used to this.
You play smart but in the end it is alweys to hard to give straight answer. Waist of time with you guys.


What a nice way to thank people who are spending their valuable time trying to help you!!!!!!

If that is the way you are grateful with people helping you, I'll certainly not waste my time any further.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives