in reply to Re: Requiring backtracking a certain number of times
in thread Requiring backtracking a certain number of times

In what I'm trying to do, 6491 would turn into 20, not 2. I just want the result of adding the numbers. But, what I really want is some way of addressing all the characters in a string/number, apply some function to it that returns a number, then add those numbers up. Ideally (because this is something that's useful in golfing), it would be something really small.

And, yes, I remember TPR(0,1) and magic numbers. :-)

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

  • Comment on Re: Re: Requiring backtracking a certain number of times

Replies are listed 'Best First'.
Re: Re: Re: Requiring backtracking a certain number of times
by Juerd (Abbot) on Apr 04, 2002 at 17:46 UTC

    what I really want is some way of addressing all the characters in a string/number, apply some function to it that returns a number, then add those numbers up.

    $_=6491; sum(some_function(split//));
    Use your favourite sum(), and your favorite some_function() ;)

    These things are very hard, and I think completely impossible with regexes.
    Although...
    s/(\d)/$s+=$1/ge; # $s is the sum... # Which leaves garbage in $_, but is a single stroke shorter than: $s+=$_ for split//; # But thenagain, this is even shorter: $s+=$_ for/./g;

    U28geW91IGNhbiBhbGwgcm90MTMgY
    W5kIHBhY2soKS4gQnV0IGRvIHlvdS
    ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
    geW91IHNlZSBpdD8gIC0tIEp1ZXJk
    

      I went down this same track, and came up with a version that doesn't munge $_:

      s/(\d)/$s+=$1,$1/ge;
      Although I was hampered by the fact that this doesn't work in psh for some reason.

      Alan

        After playing, I came up with:
        @a=/.(?{$s+=$&})/g;
        However, I have to have the @a=. If you run it without that, it only deals with the first number. *blinkblink* That's just so counter-intuitive.

        ------
        We are the carpenters and bricklayers of the Information Age.

        Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

Re: Re: Re: Requiring backtracking a certain number of times
by I0 (Priest) on Apr 05, 2002 at 03:24 UTC
    unpack"%32C*",pack "C*",split//
      Ok. I'm a dunce with pack and unpack. How on earth does this work?

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.