Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: porting C code to Perl

by Mr. Muskrat (Canon)
on Oct 23, 2017 at 16:53 UTC ( #1201910=note: print w/replies, xml ) Need Help??


in reply to porting C code to Perl

haukex and syphilis have already provided some good answers but I thought that I would point out some things that I didn't see mentioned.

Perl's int is not the same as C's floor(). What you want is POSIX::floor. It has the identical behavior as C's floor().

Perl has a pre-decrement just like C.

# for(int i = len; i > 0; --i) { # C # for(my $i = $len; $i > 0; $i--){ # incorrect Perl for(my $i = $len; $i > 0; --$i){ # correct Perl

YMMV as I haven't tried it out with these changes implemented.

Replies are listed 'Best First'.
Re^2: porting C code to Perl
by Discipulus (Monsignor) on Oct 23, 2017 at 19:55 UTC
    Hello Mr. Muskrat and thanks for your reply,

    > Perl's int is not the same as C's floor()..

    I already admitted my superb ignorance but i read here that in C floor turns 1.6 1.2 2.8 2.3 into 1 1 2 2 or simply 'This function returns the largest integral value not greater than x'.

    This is identical to the int function where any decimal are stripped out: say int $_ for qw(1.6 1.2 2.8 2.3)

    > Perl has a pre-decrement just like C..

    I have meditated a bit over this before trying to port the code and, sincerely I didnt read the C documents about post and pre increment, but as I have always understood the third part of the Perl's C style for loop is a statement on it's own: ie ++$var; or $var++; are identical.

    Infact for($i=0;$i<4;$i++){say $i} and for($i=0;$i<4;++$i){say $i} produce the very same, expectable results. Switching between them in the above code does not change the result.



    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      the third part of the Perl's C style for loop is a statement on it's own: ie ++$var; or $var++; are identical.

      Yes, that's correct*, when they're written standalone as in for(;;$var++). ++$var vs. $var++ only makes a difference when its return value is used: ++$var will first increment the variable and then return the new value, while $var++ will return the old value and then increment the variable.

      $ perl -le 'for ( my $x=0; $x<3; print $x++ ) { }' 0 1 2 $ perl -le 'for ( my $x=0; $x<3; print ++$x ) { }' 1 2 3

      * Update: In fact, note what Perl does here (edited for brevity):

      $ perl -MO=Deparse -e 'for ( my $x=0; $x<3; print $x++ ) { }' for (my $x = 0; $x < 3; print $x++) { (); } $ perl -MO=Deparse -e 'for ( my $x=0; $x<3; print ++$x ) { }' for (my $x = 0; $x < 3; print ++$x) { (); } $ perl -MO=Deparse -e 'for ( my $x=0; $x<3; ++$x ) { }' for (my $x = 0; $x < 3; ++$x) { (); } $ perl -MO=Deparse -e 'for ( my $x=0; $x<3; $x++ ) { }' for (my $x = 0; $x < 3; ++$x) { (); }
      For this particular use case, it doesn't matter, but with negative numbers, int and floor give different results (this is mentioned explicitly in the first link)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1201910]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (11)
As of 2018-07-20 13:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (431 votes). Check out past polls.

    Notices?