in reply to Re: Eek! goto?
in thread Eek! goto?

Neat peice of golf:)

Trouble is, although I probably misled you by the return ($a+$b+$c); in the benchmark code. I need the three values ($a, $b, $c) seperately in the real code.

Adding them together to return them was just a lazy way of making sure that the different methods gave the same results. I should have returned a list.


Examine what is said, not who speaks.

The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

Replies are listed 'Best First'.
Re**3: Eek! goto?
by belg4mit (Prior) on Feb 12, 2003 at 03:21 UTC
    Actually no, it was me just not paying attention. You could always do
    $H{('a'x4,'b'x4,'c'x3)[$len]} += $k[$len -1] << (8* (($len>8?$len:$len +-1)%4)); #OR no code change++ but symbolic-- ${('a'x4,'b'x4,'c'x3)[$len]} += $k[$len -1] << (8* (($len>8?$len:$len- +1)%4));

    --
    I'm not belgian but I play one on TV.

      I've tried to make this work. I added the implied while loop and put brackets around the 'a' etc. to make it generate lists not strings, but I still can get it to produce the same results as the original. Can you see what I missed?

      sub belg4mit { my %H; my ($len, @k) = @_; my ($a,$b,$c) = (0)x3; while($len--) { $H{(('a')x4,('b')x4,('c')x3)[$len]} += $k[$len -1] << (8* ( ($len > 8 ? $len : $len - 1 )%4 ) +); } return ($H{a}||0)+($H{b}||0)+($H{c}||0); }

      Examine what is said, not who speaks.

      The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.

        while($len){ $H{(('a')x4,('b')x4,('c')x3)[--$len]} += $k[$len] << (8* ( ($len > 7 ? $len +1 : $len)%4 ) ); }
        This seems to match the output of pfaut's code. If benchmarking you could eek out some more throughput by storing the hash key indexes in an array (as opposed to forcing three x per iteration of the loop.

        --
        I'm not belgian but I play one on TV.