in reply to (duplicate) Eek! goto?
Admittedly very redundant and not very graceful. Let's look at this another way: you have a basically data driven design here: the target and shift factor depends on the index. So not write it that way?{ last if $len == 0; $a+= $k[0]; last if $len == 1; $a+= $k[1] <<8; last if $len == 2; $a+= $k[2] <<16; last if $len == 3; $a+= $k[3] <<24; last if $len == 4; $b+= $k[4]; last if $len == 5; $b+= $k[5] <<8; last if $len == 6; $b+= $k[6] <<16; last if $len == 7; $b+= $k[7] <<24; last if $len == 8; $c+= $k[8] <<8; last if $len == 9; $c+= $k[9] <<16; last if $len == 10; $c+= $k[10] <<24; }
or maybemy @targ_shift = ( [ \$a, 0 ], [ \$a, 8 ], [ \$a, 16 ], [ \$a, 24 ], [ \$b, 0 ], [ \$b, 8 ], [ \$b, 16 ], [ \$b, 24 ], [ \$c, 8 ], [ \$c, 16 ], [ \$c, 24 ], ); for my $idx (0 .. $len-1) { my ($targ, $shift) = @{$targ_shift[$idx]}; $$targ += $k[$idx] << $shift; }
"Capture regularities in code, irregularities in data", the koan of the senior programmer as merlyn so aptly put it.{ my @targ_shift = ( \$a, 0, \$a, 8, \$a, 16, \$a, 24, \$b, 0, \$b, 8, \$b, 16, \$b, 24, \$c, 8, \$c, 16, \$c, 24, ); for (0 .. $len-1) { my ($targ, $shift) = splice @targ_shift, 0, 2; $$targ += $k[$_] << $shift; } }
Makeshifts last the longest.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Eek! goto?
by BrowserUk (Patriarch) on Feb 19, 2003 at 13:57 UTC | |
by Aristotle (Chancellor) on Feb 19, 2003 at 14:31 UTC |