Perl-Sensitive Sunglasses PerlMonks

### reducing the number of lines

by baxy77bax (Deacon)
 on Jan 11, 2013 at 15:59 UTC Need Help??
baxy77bax has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have a mental blockade and i need to do this in the next hour. I need to rewrite this in a more simpler form but however i substitute the variables i get the same number of lines being executed:

```if(\$l<\$k){
if(\$s_rev[\$d+1]==0 && \$k-2>0){
\$s_rev[\$d+1] = \$k-2;
}
\$h=\$s[\$b] == 0 ? 0 : \$s[\$b]-1;
\$s[\$b1] = max(\$k,\$lt);
\$c=2;
\$j++;
}
elsif(\$l>\$k){
if(\$s[\$b+1]==0 && \$l-2>0){
\$s[\$b+1] = \$l-2;
}
\$h=\$s[\$b] == 0 ? 0 : \$s[\$b]-1;
\$s[\$b1] =max(\$l,\$kt);
\$c=1;
\$i++;
}
else{
if(\$s[\$b+1]==0 && \$k-2>0){
\$s[\$b+1] = \$k-2;
}
\$s[\$b1] = max(\$k,\$lt);
\$h=0;
\$c=0;
}

sub max{

}
so there are 3 conditions which are very similar in fact the first two are the same with opposite variables.

Thank you !

Update: all variables are numbers or number arrays. moreover, they are integers. So this is a procedure that a nee to write down as pseudo code with as less executable lines as possible

Replies are listed 'Best First'.
Re: reducing the number of lines
by choroba (Bishop) on Jan 11, 2013 at 16:59 UTC
Why do you need to simplify the code? Reducing the number of lines is not easy, but refactoring the similar parts to subroutines is possible:
```sub assign_max {
my (\$x, \$y) = @_;
\$s[\$b1] = max(\$x, \$y);
}

sub compare {
my (\$ar, \$idx, \$subst) = @_;
if (\$ar->[\$idx] == 0 && \$subst > 0) {
\$ar->[\$idx] = \$subst;
}
}

\$h = \$s[\$b] == 0 ? 0 : \$s[\$b] - 1 unless \$l == \$k;
\$c = (0, 1, 2)[\$l <=> \$k];

if (\$l < \$k) {
compare(\@s_rev, \$d + 1, \$k - 2);
assign_max(\$k, \$lt);
\$j++;

} elsif (\$l > \$k) {
compare(\@s, \$b + 1, \$l - 2);
assign_max(\$l, \$kt);
\$i++;

} else {
compare(\@s, \$b + 1, \$k - 2);
assign_max(\$k, \$lt);
\$h = 0;
}
As you can see, assign_max uses @s and \$b1 as globals.
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: reducing the number of lines
by ww (Archbishop) on Jan 11, 2013 at 16:22 UTC

This has fewer lines:

```if(\$l<\$k){ if(\$s_rev[\$d+1]==0 && \$k-2>0){\$s_rev[\$d+1] = \$k-2; } \$h=\$s[
+\$b] == 0 ? 0 : \$s[\$b]-1;
\$s[\$b1] = max(\$k,\$lt); ....

I suspect -- no, know damn well -- that it's not helpful. However, the fact is, since I'm in no position to guess what your inputs are like, nor to intuit your actual goal, ...well, it's the best I can do for now.

Please, update with more details (data sample, narrative intent)!

Re: reducing the number of lines
by tobyink (Abbot) on Jan 11, 2013 at 17:06 UTC

Without any test data, it's hard to tell it this runs the same as your original, but...

```my (\$max, \$cval, \$inc_var, \$sref, \$d_or_b) =
(\$l == \$k) ? ([\$k,\$lt], 0, undef, \@s,     \$b) :
(\$l <  \$k) ? ([\$k,\$lt], 2, \\$j,   \@s_rev, \$d) :
([\$l,\$kt], 1, \\$i,   \@s,     \$b);
\$sref->[\$d_or_b+1] ||= \$max->[0] - 2 if \$max->[0] > 2;
\$h                   = \$s[\$b] == 0 ? 0 : \$s[\$b]-1;
\$s[\$b1]              = max(@\$max);
\$c                   = \$cval;
\$\$inc_var++  if \$inc_var;
\$h = \$c = 0  if ( \$l==\$k );

Update #1: Thanks choroba, colons fixed.

Update #2: Thanks choroba again for the test script template; minor adjustments, works now.

perl -E'sub Monkey::do{say\$_,for@_,do{(\$monkey=[caller(0)]->[3])=~s{::}{ }and\$monkey}}"Monkey say"->Monkey::do'
Aren't you missing a colon somewhere?
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: reducing the number of lines
by pvaldes (Chaplain) on Jan 12, 2013 at 13:26 UTC

I need to rewrite this in a more simpler form

```\$k-2>0
\$l-2>0

Same as:

```\$k>2
\$l>2

And you can save one line here

```\$h=0;
\$c=0;

same as:

(\$h,\$c) = (0,0);

Updated: the tobyink's solution is more elegant

\$h = \$c = 0
Re: reducing the number of lines
by sundialsvc4 (Abbot) on Jan 11, 2013 at 17:33 UTC

Let me offer a contrarian opinion:   why not use what you have originally written?   Right now (except for the cryptic variable-names), even though there is obvious redundancy among the various cases, the redundancy is obvious, and each of the separate cases can readily be compared by eye.   Furthermore, right now, none of them are “coupled” to one another:   each one could, should the need arise, be revised arbitrarily without impacting any other.   Never underestimate the creativity of the Marketing Department to throw a monkey-wrench into the barrel of cleverness.   “Reducing the number of lines” might not be a good thing to do; it could sometimes even be an ill-advised thing to do.

Re: reducing the number of lines
by choroba (Bishop) on Jan 11, 2013 at 17:25 UTC
For those interested, this is the code I used to compare the new solution to the original one:
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Create A New User
Node Status?
node history
Node Type: perlquestion [id://1012910]
Approved by marto
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2018-05-25 22:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
World peace can best be achieved by:

Results (191 votes). Check out past polls.

Notices?