#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2000
    Location
    Frederick, Md, US
    Posts
    0
    Rep Power
    0
    I want to jump over a part or parts of the program on a condition. i have seen reference to a 'goto' function, but its use is discouraged and there is no discussion of how to use it. basically i have several hundred conditionals. right now i run through them all, but i want to be able to skip whole sets of them if a certain condition is met, or if another condition is not met.


    so if want to test

    if ( $debt_ratio_back > 50)

    then --goto somewhere--
    else keep going


    if (
    ( $aa{LoanTransaction} =~ /Purchase/
    | | $aa{LoanTransaction} =~ /RTRefinance/)
    && $aa{IncomeDocs} =~ /FullDoc/
    # && $aa{Employment} =~ /SE/
    && ( $aa{PropertyType} =~ /SFD/
    | | $aa{PropertyType} =~ /TH/
    | | $aa{PropertyType} =~ /Condo/
    | | $aa{PropertyType} =~ /2U/
    | | $aa{PropertyType} =~ /MH/)
    && $aa{PropertyOccupancy} =~ /PR/
    && $aa{CreditScore} >= 681
    && $aa{M30Lates} == 0
    && $aa{M60Lates} == 0
    && $aa{M90Lates} == 0
    && $aa{M30Last24No} =~ /No/
    && $aa{BK} >= 4
    && $aa{CH13Open} =~ /No/
    && $aa{CollLast2} =~ /No/
    # && $aa{CondProp} =~ /ave/
    && $debt_ratio_front <= 35
    && $debt_ratio_back <= 45
    && $aa{CreditLines24} >= 5
    && $aa{RI30LessMore2} <= 2
    && $aa{I30Last12} <= 0
    && $aa{I60Last12} == 0
    && $aa{I90Last12} == 0
    && $aa{R30Last12} <= 0
    && $aa{R60Last12} == 0
    && $aa{R90Last12} == 0
    )

    somewhere

    i would have a bunch of this type of thing. I see warnings regarding spaghetti code. what are my options.

    thanks for any help
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2000
    Location
    Indiana
    Posts
    614
    Rep Power
    15
    Yeah, back before they had functions and OO programming, they used goto all the time and you would end up jumping all over your program based on conditions. This was not only mind bogoling to keep track of, but it also created lots of spageti code.

    You want to do it in a sub routine, like this (simple example):
    ----------------

    $variable='fdstesttest';

    $do=&check_content($variable);

    if(defined(&$do)){&$do}else{die "No $do subroutine"}

    sub check_content {
    my $check=shift;
    if($check=~/blah/){return 'blah'}
    if($check=~/test/){return 'asdf'}
    return 'nothin';
    }

    sub blah {
    print "BLAH!";
    }

    sub asdf {
    print "asdf!";
    }

    sub nothin {
    print "I didn't find anything!";
    }
    -------

    Now this is a bit of a roundabout way of doing it, but just know that 'return' exits a subroutine.. and if a value is specified, it will return that value. In this case, it would run the asdf subroutine (because it would match 'test' from the variable).
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Location
    Southern California
    Posts
    73
    Rep Power
    15
    [QUOTE]Originally posted by ohopp:
    I want to jump over a part or parts of the program on a condition. i have seen reference to a 'goto' function, but its use is discouraged and there is no discussion of how to use it.[QUOTE]

    You can test for failure like this:

    for (keys %aa) {

    $err = 1, last if /^LoanTransaction$/ and $aa{$_} !~ /^(Purchase|RTRefinance)$/;

    ... etc.
    }

    if ($err) ...

    Or you can use constraint subs:

    for (keys %aa) {

    $err = 1, last unless loan_tr();
    $err = 1, last unless ...
    }

    sub loan_tr { return ($_[0] =~ /^(Purchase|RTRefinance)$/); }



IMN logo majestic logo threadwatch logo seochat tools logo