On 12/03/2011 12:36 PM, Martti Laaksonen wrote:
[[ ${#str} -gt ${lim} ]]&& { begin=${str:0:$((lim-rem))}; end=${str:$((lim-rem)):${rem}}; echo ${begin}${end//?/${char}}; } || echo "${str}"
OK, That is pretty darn slick! Thanks Martti. I wouldn't have thought about string substitution using a single char wildcard as the substring no matter how many times I looked at ${string//substring/replacement} :) Now the question becomes -- which takes less clock cycles? ANSWER - both so close it's hard to call even with 100 repeated calls. For those interested a brief synopsis of the testing was: Hmm... elm.sh=Martti's way; eld.sh=David's way (first run nothing in cache): 10:15 providence:~/scr/dev> time sh elm.sh my-very-long-string-of-stuff 24 4 + my-very-long-string-++++ real 0m0.010s user 0m0.003s sys 0m0.003s 10:17 providence:~/scr/dev> time sh eld.sh my-very-long-string-of-stuff 24 4 + my-very-long-string-++++ real 0m0.011s user 0m0.007s sys 0m0.003s Martti's wins -- now let's run it a second time with the files in cache: 10:17 providence:~/scr/dev> time sh elm.sh my-very-long-string-of-stuff 24 4 + my-very-long-string-++++ real 0m0.010s user 0m0.007s sys 0m0.003s 10:19 providence:~/scr/dev> time sh eld.sh my-very-long-string-of-stuff 24 4 + my-very-long-string-++++ real 0m0.010s user 0m0.003s sys 0m0.003s Now -- I just don't get it -- results are opposite. (although the times are both so small as to be in the noise) Oh well, I still like the elegance of the string substitution that eliminates the for loop. OK, what about 100 times: time for ((i=0;i<100;i++)); do sh elm.sh my-very-long-string-of-stuff 24 4 +; done real 0m1.021s user 0m0.587s sys 0m0.220s time for ((i=0;i<100;i++)); do sh eld.sh my-very-long-string-of-stuff 24 4 +; done real 0m1.005s user 0m0.577s sys 0m0.220s For those interested, the test code was: 10:19 providence:~/scr/dev> cat elm.sh #!/bin/bash function emartti() { # validate sufficient parameters passed [[ -z "$1" ]] || [[ -z "$2" ]] && { echo "ERROR: Insufficient input in fxn emartti"; return 1; } str="${1}" lim="${2}" rem=${3:-3} char=${4:-.} # validate integers [ $lim -eq $lim ] 2>/dev/null || return 1 [ $rem -eq $rem ] 2>/dev/null || return 1 [[ ${#str} -gt ${lim} ]] && { begin=${str:0:$((lim-rem))}; end=${str:$((lim-rem)):${rem}}; echo ${begin}${end//?/${char}}; } || echo "${str}" } [[ -n $4 ]] && { emartti "$1" "$2" "$3" "$4" exit 0 } [[ -n $3 ]] && { emartti "$1" "$2" "$3" exit 0 } [[ -n $2 ]] && { emartti "$1" "$2" exit 0 } [[ -z $1 ]] || [[ -z $2 ]] && { echo "ERROR: Insufficient input in fxn emartti"; exit 1; } exit 0 10:22 providence:~/scr/dev> cat eld.sh #!/bin/bash ellipse() { # validate sufficient parameters passed [[ -z "$1" ]] || [[ -z "$2" ]] && { echo "ERROR: Insufficient input in fxn ellipse"; return 1; } str="$1" lim="$2" end=${3:-3} chr=${4:-.} # validate integers [ $lim -eq $lim ] 2>/dev/null || return 1 [ $end -eq $end ] 2>/dev/null || return 1 [[ ${#str} -gt $lim ]] && { newstr=${str:0:$((lim-end))} for((i=$((lim-end));i<$lim;i++)); do newstr=${newstr}${chr} done echo "$newstr" } || { echo "$str" } } [[ -n $4 ]] && { ellipse "$1" "$2" "$3" "$4" exit 0 } [[ -n $3 ]] && { ellipse "$1" "$2" "$3" exit 0 } [[ -n $2 ]] && { ellipse "$1" "$2" exit 0 } [[ -z $1 ]] || [[ -z $2 ]] && { echo "ERROR: Insufficient input in fxn ellipse"; exit 1; } exit 0 -- David C. Rankin, J.D.,P.E. -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org To contact the owner, e-mail: opensuse+owner@opensuse.org