in reply to Eek! goto?

Untested but how about
$a += $k[$len -1] << (8* (($len>8?$len:$len-1)%4));

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

Replies are listed 'Best First'.
Re: Re: Eek! goto?
by BrowserUk (Patriarch) on Feb 12, 2003 at 02:57 UTC

    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.

      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.