5 filename="${1:-example.txt}"
14 declare -A bracket_scores=( [")"]=3 ["]"]=57 ["}"]=1197 [">"]=25137 )
15 declare -A bracket_complete_score=( [")"]=1 ["]"]=2 ["}"]=3 [">"]=4 )
16 declare -a completion_scores
18 while read -u 3 line; do
21 for (( a=0; a<${#line}; a++ )); do
37 if [ $char != ${brackets[-1]} ]; then
38 echo "Syntax error, bracket: $char, expected ${brackets[-1]} at offset $a on line $line_number"
39 ((score+=${bracket_scores[$char]}))
42 # we're now skipping the rest of the line
45 # all good, so remove the last element of the array
51 if [ $bad_line -eq 0 ]; then
52 incomplete_lines+=("$line")
54 # usefully, brackets will still contain the right things
55 while [ ${#brackets[@]} -gt 0 ]; do
56 ((completion_score*=5))
57 ((completion_score+=${bracket_complete_score[${brackets[-1]}]}))
60 completion_scores+=($completion_score)
64 echo "Score for file: $score"
66 # sort completion_scores
68 completion_scores=($(sort -n <<<"${completion_scores[*]}"))
69 # now get the middle value
70 mid_completion=$((${#completion_scores[@]} / 2))
71 echo "Completion score: ${completion_scores[$mid_completion]}"