LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Complex regex (Sed/perl) (https://www.linuxquestions.org/questions/programming-9/complex-regex-sed-perl-939789/)

Nominal Animal 04-15-2012 10:08 AM

Quote:

Originally Posted by battler (Post 4653269)
It's almost perfect, I get a "division by zero attempted" attempt on row 38 in the script.

I think you used the original version; I overlooked the "asterisk may be at the start of the next line instead of the end of the answer line" -issue. That I fixed in edit 1. In edit 2, I added logic that marks the final answer correct if none of the others were correct. (Oh, I'd better add a warning to standard output if that happens; that'll be marked edit 3.)

grail 04-15-2012 11:17 AM

Well mine isn't quite as in depth as NAs, but it seems to work on the data provided:
Code:

#!/usr/bin/awk -f

/^[1-9]/{
    print "::",gensub(/^[^[:alpha:]]+| \[.*/,"","g")
    print "::",gensub(/^[^[:alpha:]]+/,"","1")
    print "{"
}

/^[a-z]/{
    answer[++cnt] = substr($0,4)

    if(/\*/){
        correct[cnt]++
        answer[cnt] = substr(answer[cnt],1,length(answer[cnt])-1)
    }
    next
}

cnt && (/\*/ || /^$/){
    percentage = 0

    if(/\*/)
        correct[cnt]++

    for(x = 1; x <= cnt; x++){
        if(percentage)
            if(correct[x])
                printf "~%%%.2f%% %s\n",percentage,answer[x]
            else
                printf "~%%100%% %s\n",answer[x]
        else
            if(correct[x])
                print "=",answer[x]
            else
                print "~",answer[x]
    }

    print "}"

    cnt = 0
    delete correct
    delete answer
}



All times are GMT -5. The time now is 03:48 AM.