views:

43

answers:

1

Hi, i am trying to change this:

foreach $require (@Required) {

        # If the required field is the email field, the syntax of the email  #
        # address if checked to make sure it passes a valid syntax.          #
        if ($require eq 'email' && !&check_email($Config{$require})) {
            push(@error,$require);
        }

//////////////////////////////////////////////////////////////////////////////////

sub check_email {
    # Initialize local email variable with input to subroutine.              #
    $email = $_[0];

    # If the e-mail address contains:                                        #
    if ($email =~ /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/ ||

        # the e-mail address contains an invalid syntax.  Or, if the         #
        # syntax does not match the following regular expression pattern     #
        # it fails basic syntax verification.                                #

        $email !~ /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z0-9]+)(\]?)$/) {

        # Basic syntax requires:  one or more characters before the @ sign,  #
        # followed by an optional '[', then any number of letters, numbers,  #
        # dashes or periods (valid domain/IP characters) ending in a period  #
        # and then 2 or 3 letters (for domain suffixes) or 1 to 3 numbers    #
        # (for IP addresses).  An ending bracket is also allowed as it is    #
        # valid syntax to have an email address like: user@[255.255.255.0]   #

        # Return a false value, since the e-mail address did not pass valid  #
        # syntax.                                                            #
        return 0;
    }

    else {

        # Return a true value, e-mail verification passed.                   #
        return 1;
    }
}

into this:

foreach $require (@Required) {


            if ($require eq 'fieldb' && !&check_fieldb($Config{$require})) {
                push(@error,$require);
            }

    ///////////////////////////////////////////////////////////////////////////////

    sub check_fieldb {

        # If field b is under 20% of field a:                                        #
        if ($fieldb <=($fielda/100)*20 ) {

            # Return a false value, since field b is less than 20% of field a
            return 0;
        }

        else {

            # Return a true value, fieldb verification passed.                   #
            return 1;
        }
    }

but it does not work, always returns as 0. how would i fix this?

A: 

It's impossible to be sure what's wrong without knowing the values of $fielda and $fieldb. My diagnosis is that $fieldb is less than or equal to ($fielda/100)*20

You pass a value to check_fieldb, but you never use it. Why do you pass it? As a commenter noted you should be passing to the function the values you want to check. Are $fielda and $fieldb guaranteed to be correctly initialized before check_fieldb is called?

Do you meant to be saying

foreach my $require (@Required){
    if($require eq 'fieldb' && !check_fieldb($value_of_fielda, $value_of_fieldb)){
        push(@error, $require);
    }
}

# ... later ...

sub check_fieldb($$){
    my $fielda = shift;
    my $fieldb = shift;

    return !($fieldb <=($fielda/100)*20);
}

perhaps?

Sorpigal
i originally tried with fielda as 10 and fieldb as 2, which returned 0 like it should, but if i change the numbers to anything else they still return 0 when it should return 1 if it doesn't have a value 20% of fielda
Jamie
Why the template in the check_fieldb subroutine?
ishnid
fielda and fieldb are two fields entered in a form, so they could be anything. the check is supposed to see if fieldb is less than or equal to 20% of field a and if it is it goes to the error page. If not the field validates
Jamie
the above isn't working for me, wish i could explain the problem better but this is the first time i have ever really looked at perl
Jamie
the error message was caused by this part : foreach my $require (@Required){ if($require eq 'fieldb' } }
Jamie
@ishnid: Prototypes are a good idea, that's why. @Jamie: I've never run or tested this code in any capacity, but it's probably complaining about not knowing the prototype for check_fieldb. If so you can add `sub check_fieldb($$);` above the foreach loop to correct this, or just remove the `($$)` from the definition of check_fieldb, or move the deceleration of check_fieldb to before its first call.
Sorpigal