Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
Perl: the Markov chain saw
 
PerlMonks  

reducing the number of lines

by baxy77bax (Chaplain)
on Jan 11, 2013 at 15:59 UTC ( #1012910=perlquestion: print w/ replies, xml ) 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

thank for the reply ww

Comment on reducing the number of lines
Download Code
Re: reducing the number of lines
by ww (Bishop) 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 choroba (Abbot) 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 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 choroba (Abbot) 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:
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: reducing the number of lines
by sundialsvc4 (Monsignor) 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 pvaldes (Hermit) 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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1012910]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (11)
As of 2014-04-18 14:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (469 votes), past polls