in reply to A little C code converted to Perl *HELP*

Maybe you should tell us what the code *does*. I'd have a *much* easier time writing Perl code from scratch given a description in English of what it's supposed to do. I've taken a shot at reading the C, but my C is marginal in the extreme, so I very well could get something wrong here...

As near as I can determine, all this code does is seed the random number generator. Correct me if I am mistaken (did I mention my C is marginal?), but that is the only effect I think it has. If that is the case, you can translate it into a comment that indicates that Perl does this automatically.

If the C code is doing something more substantial, you'll have to tell us what it's doing, before we can tell you how to do that in Perl. We're _Perl_ gurus; many of us don't know any C at all. If you need help with C, this really isn't the best place to get it.


$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/

Replies are listed 'Best First'.
Re: Re: A little C code converted to Perl *HELP*
by regnab (Initiate) on Nov 03, 2003 at 04:34 UTC
    yep i think you are right, would you mind give me this little perl code which will make a random gcodeid like the c script do... you would do me a world-big-favour ;))

      Oh, I almost missed that little part at the end with the sprintf...

      DWORD generated_check=(rand()<<16) | rand(); char gcheck[64]; sprintf(gcheck, "%x", generated_check);

      My C, as I said, is marginal, but if this is doing what I think it's doing, ...

      $gcheck = printf "%x", ((rand() << 16) | rand());

      I'm assuming here that the C << operator is a left shift just like in Perl, but that's a wild guess. I'm also assuming that rand with no args in C does the same thing as in Perl, which is also a guess. Like I said, my knowledge of C leaves something to be desired. I know what the Perl code does. (It prints "0" every time.) I'm guessing at the C code. Probably guessing wrong.


      $;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/

        The rand function in C returns a random integer. The OP's C code seems to assume 16 bit integers, so that's a max of 65535 (or 32767 to -32768, IIRC). So, I think the equivalent Perl code would be something like:

        $gcheck = sprintf "%x", ((int(rand(65536)) << 16) | int(rand(65536)));

        bbfu
        Black flowers blossom
        Fearless on my breath

        okay, great many thanks mate!!

        (if someone thinks this wasnt correct, pls reply another solution)

        Thanks again mate!!! :))
        hmmm cant get that line working you gave me .. always give me 0 , or 1 ...

        i think somethings wrong there :( hmmmm

        any solution code? many thx