Re: if statement consolidation
by moritz (Cardinal) on Jun 27, 2012 at 15:45 UTC
|
You can always do the data-driven approach, for example
my %inits = (
1 => [1, 0, 9],
2 => [2, 1, 10],
3 => [3, 0, 11],
# rest of the values go here
);
if ($inits{$h}) {
my @i = @{ $inits{$h} };
$j = $h + $i[0];
$p = $i[1];
$v = $i[2];
}
Or even
my %inits = (
1 => [2, 0, 9],
2 => [5, 1, 10],
3 => [8, 0, 11],
# rest of the values go here
);
($i, $p, $v) = @{$inits{$h}} if $inits{$h};
| [reply] [d/l] [select] |
Re: if statement consolidation
by choroba (Cardinal) on Jun 27, 2012 at 15:49 UTC
|
my %changes = (1 => [1, 0, 9],
3 => [2, 1, 10],
5 => [3, 0, 11],
7 => [4, 1, 12],
10 => [5, 0, 5],
12 => [6, 1, 6],
14 => [7, 0, 7],
16 => [8, 1, 8],
);
if (exists $changes{$h}) {
$j = $h + $changes{$h}->[0];
$p = $changes{$h}->[1];
$v = $changes{$h}->[2];
}
| [reply] [d/l] |
Re: if statement consolidation
by muba (Priest) on Jun 27, 2012 at 15:48 UTC
|
If your Perl is relatively modern, you could look into given/when. Another idea might to write a look-up hash and draw from that. I'm not saying it's the best idea, but at least it's an idea. Update: and a rather popular idea, too.
my %values_for_h =>
1 => [1, 0, 9],
3 => [2, 1, 10],
5 => [3, 0, 11],
7 => [4, 1, 11],
10 => [5, 0, 5],
12 => [6, 1 6],
14 => [7, 0, 7],
16 => [8, 1, 8]
);
if (exists $values_for_h{$h}) {
$j = $h + $values_for_h{$h}->[0];
$p = $values_for_h{$h}->[1];
$v = $values_for_h{$h}->[2];
}
| [reply] [d/l] |
Re: if statement consolidation
by choroba (Cardinal) on Jun 27, 2012 at 16:19 UTC
|
if ($h > 0 and $h <= 16 and $h % 2 == $h < 9) {
my $n = int(($h + 1) / 2);
$j = $h + $n;
$p = 0 + ! ($n % 2);
$v = $n + (8 * ($h < 10));
}
Update: "if" condition added. | [reply] [d/l] |
|
|
| [reply] |
|
|
| [reply] |
Re: if statement consolidation
by BrowserUk (Patriarch) on Jun 27, 2012 at 15:53 UTC
|
my %lookup = (
1, [ 2, 0, 9 ],
3, [ 5, 1, 10 ],
5, [ 8, 0, 11 ],
7, [ 11, 1, 12 ],
10,[ 15, 0, 5 ],
12,[ 18, 1, 6 ],
14,[ 21, 0, 7 ],
16,[ 24, 1, 8 ],
);
my( $j, $p, $v ) = @{ $lookup{ $h } };
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
| [reply] [d/l] |
Re: if statement consolidation
by bulk88 (Priest) on Jun 28, 2012 at 00:54 UTC
|
Use "data driven" as described in this thread. Or use XS, a C switch statement is orders of magnitude smaller in memory than your code, especially if all these numbers fit in the range of a char. For a pure perl solution, to reduce opcodes (nextstates and sassigns), convert a
$j=$h+8;
$p=1;
$v=8;
to (one aassign op)
($j, $p, $v) = ($h+8, 1, 8);
or fatter but faster (no nextstates, but multiple sassign might be faster one aassign a profiler, I dont remember from last time I tried to optimize away nextstate ops)
($j = $h+8),($p = 1), ($v = 8);
note, I didn't B::Concise this post. | [reply] [d/l] [select] |
Re: if statement consolidation
by RichardK (Parson) on Jun 27, 2012 at 18:31 UTC
|
If I may say, that's a pretty ugly if , IMHO ;)
So maybe that's a signal that you're going about it in the wrong way. Try restating the original problem and think of a different approach.
| [reply] |
Re: if statement consolidation
by Anonymous Monk on Jun 27, 2012 at 20:33 UTC
|
There is also something to be said for clarity. The only real caution here is that you probably want to be using elsif especially if there is any chance whatsoever that the value of $h could be modified within any of the if-blocks. Even though the code as written (with the elsif change) is wordy and repetitious, it is also easy to understand and modify, and each alternative block of code stands alone. You could, if you chose and if you needed to, do completely separate and unrelated things in each block: there is no functional correlation or "tying" of any kind between them. If that works for you, all things properly considered, then do it without remorse. it is not "wrong." | [reply] |
Re: if statement consolidation
by linuxkid (Sexton) on Jun 27, 2012 at 23:02 UTC
|
given($h) {
when (2) {...}
}
--linuxkid
imrunningoutofideas.co.cc
| [reply] [d/l] |