in reply to (Golf) Kaprekar's Process

Aarrrgghhhh!!! I'm going crazy here. I can't seem to figure out why something is working the way it is; could any of you help me? I left all my debugging print statements there so that you can run it yourself and see the same results.
sub b { print 'args=',join(",",@_),"\n"; $,=pop()-pop(); print ", = $,\n"; $x=join'',sort split//,$,; print "x = '$x'\n"; ## the line below doesn't seem to work!! @_=map{scalar reverse($_),$_ } $x; print "\@_ = ",join(",",@_),"\n"; print "\n"; $,==495?0:1+b(@_); } sub c { @a=map{ scalar reverse($_),$_ } pop; print join(",",@a),"\n"; } c(312); b(312); ==== Results when run< ==== 213,312 ## this is c() args=312 ## this is b() , = 312 x = '123' @_ = 312321,123312
For some reason, the map statement in b() is resulting in that strange "312321,123312" results ... which for the life of me, I can't figure out why.

I have a feeling it's something stupid, but I can't see it. Can anyone else?

Update: God, I love this place. You write a technical, perl question, one that contains a user-error (such as I did, using $, as a temporary variable (golf, you know)), and you get down voted as much as upvoted. This node of mine has bounced from -1 to 1 so many times my head is spinning. So at what point is a question stupid enough that you shouldn't ask? I seem to remember quite a few people having on their tag lines, and on their tongues, the phrase of "Ask a silly question, be a fool for a minute, don't ask a silly question and be a fool forever.". I guess that doesn't always apply, huh?</rant>

mr.nick ...

Replies are listed 'Best First'.
Re: Re: (Golf) Kaprekar's Process
by sean (Beadle) on Jun 17, 2001 at 09:22 UTC
    Your map statement is working fine. The problem is that you have assigned a value to "$,", which is the output field separator variable. Its value is printed when you use a comma in a print statement.
    $,="bar"; print "foo","baz";
    will output:
Re: Re: (Golf) Kaprekar's Process
by dimmesdale (Friar) on Jun 17, 2001 at 06:05 UTC
    That's odd...I tried running this on my system. First I just did the map statement, and assigned 123 to x. This worked perfectly. So, I got a little confused as to why it worked for me, and not for you, so I tried to run the whole script. However--and this is quite a big however--it resulted in an infinite loop, printing out large, negative numbers on my machine. Is this the exact code?
      Oh, it loops indefinately, all right. I ran it | head. And yes, that is the exact code.

      If you grab the top of the output, do the see the odd value of @_ = 312321,123312? I would think that @_ would contain the same value as the output from c() since it uses the same map statement (- the pop). But I can't shake the feeling I'm missing something simple here (as I often do) :)

      Btw, I split the subroutine up into individual statements for debugging. It used many fewer temporary variables at first.

      mr.nick ...