Sponsored Content
Full Discussion: Naive coding...
The Lounge What is on Your Mind? Naive coding... Post 302992180 by Corona688 on Wednesday 22nd of February 2017 10:58:54 AM
Old 02-22-2017
Quote:
Originally Posted by wisecracker
I have this insane distrust of compilers and interpreters.
So I do what could be called naive coding in most langauages that I know well enough because of this distrust.

This is one example of my naive code and IS actually inside AudioScope.sh.
Code:
read -r -p "Set timebase starting point. From 0 to $scan_end<CR> " -e tbinput
# Ensure the timebase values are set to default before changing.
scan_start=0
scan_jump=1
# Eliminate any keyboard error longhand...
# Ensure a NULL string does NOT exist.
if [ "$tbinput" == "" ]
then
	scan_start=0
	tbinput=0
fi
# Find the length of the inputted string and correct for subscript position.
str_len=$(( ${#tbinput} - 1 ))
# Now check for continuous numerical characters ONLY.
for count in $( seq 0 $str_len )
do
	# Reuse variable _number_ to obtain each character per loop.
	number=${tbinput:$count:1}
	# Now convert the character to a decimal number.
	number=$( printf "%d" \'$number )
	# IF ANY ASCII character exists that is not numerical then reset the scan start point.
	if [ $number -le 47 ]
	then
		scan_start=0
		tbinput=0
	fi
	if [ $number -ge 58 ]
	then
		scan_start=0
		tbinput=0
	fi
done

Derivatives of this have never failed under normal conditions on the langauges I have used so it seems idiot proof.
Would professionals like yourselvs consider this puerile coding?
That's just about the most difficult way possible to solve the problem. I only resort to it when the language features just can't handle it (i.e. needing to build a recursive parser from scratch).

When you find yourself doing this for trivial things, you're definitely overthinking it. Try inverting the problem. What if you looked for exactly one non-numeric character? You only need to find one to prove the string's bad, and if you can't... fait accompli.

One way:
Code:
case "$STR" in 
) echo "Blank" ;;
*[^0-9]*)  echo "Contains non-numeric" ;;
*) echo "Valid" ;;
esac

This is portable across all bourne shells. In BASH, you could reduce it to a single statement.
 

7 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

can I use this coding

I apologise because I had pasted this question in the newbies forum first (because i am a bit of a newbie) but thought it might be better suited in here if i have to sepearate parameters can I use this syntax especially the or part (||) and is this correct if (6 Replies)
Discussion started by: w33man
6 Replies

2. Shell Programming and Scripting

Coding on my Mac

I would like to start coding on my mac, but I'm getting an error when I attempt to execute my script -bash : testscript: command not found I have verified that the #! line points to the correct directory. If you have some insight it would be greatly appreciated! - D (1 Reply)
Discussion started by: DKNUCKLES
1 Replies

3. UNIX for Dummies Questions & Answers

pro*c coding

Hi All, I am new to pro*C. I have a select statement as select a.ename,a.sal,a.empno from emp where &n=(select count(distinct(b.sal)) from emp b where a.sal<=b.sal for this query I have to write a pro*C program. So can you please send me the complete code. Then I will foloow the same... (1 Reply)
Discussion started by: user71408
1 Replies

4. Homework & Coursework Questions

Naive Bayes

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have to write a program in Unix to do the following.Given a phrase like george hates john, by... (0 Replies)
Discussion started by: gizmo87
0 Replies

5. Shell Programming and Scripting

Need help with coding

HI, Can some one guide me how to make changes to the script below so that it can load the history of a program to IT server ? Format of data: YYYYMMDD065959.dsk.log YYYYMMDD235959.dsk.log currently both are loaded together. Need to separate them as above format. Thanks in advance. ... (1 Reply)
Discussion started by: crazydude80
1 Replies

6. Windows & DOS: Issues & Discussions

Need help with coding

HI, Can some one guide me how to make changes to the script below so that it can load the history of a program to IT server ? Format of data: YYYYMMDD065959.dsk.log YYYYMMDD235959.dsk.log currently both are loaded together. Need to separate them as above format. Thanks in advance. ... (2 Replies)
Discussion started by: crazydude80
2 Replies

7. Shell Programming and Scripting

HTTP coding

My company has an in house instant messaging system (like WhatsApp) where users can communicate with each other. I currently have code to email me certain items from my Sparc machine running SunOS 5.10. I want what I am emailing myself to now instant message me. The team that created the messenger... (5 Replies)
Discussion started by: shorty
5 Replies
echo(1B)					     SunOS/BSD Compatibility Package Commands						  echo(1B)

NAME
echo - echo arguments to standard output SYNOPSIS
/usr/ucb/echo [-n] [argument] DESCRIPTION
echo writes its arguments, separated by BLANKs and terminated by a NEWLINE, to the standard output. echo is useful for producing diagnostics in command files and for sending known data into a pipe, and for displaying the contents of envi- ronment variables. For example, you can use echo to determine how many subdirectories below the root directory (/) is your current directory, as follows: o echo your current-working-directory's full pathname o pipe the output through tr to translate the path's embedded slash-characters into space-characters o pipe that output through wc -w for a count of the names in your path. example% /usr/bin/echo "echo $PWD | tr '/' ' ' | wc -w" See tr(1) and wc(1) for their functionality. The shells csh(1), ksh(1), and sh(1), each have an echo built-in command, which, by default, will have precedence, and will be invoked if the user calls echo without a full pathname. /usr/ucb/echo and csh's echo() have an -n option, but do not understand back-slashed escape characters. sh's echo(), ksh's echo(), and /usr/bin/echo, on the other hand, understand the black-slashed escape characters, and ksh's echo() also understands a as the audible bell character; however, these commands do not have an -n option. OPTIONS
-n Do not add the NEWLINE to the output. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWscpu | +-----------------------------+-----------------------------+ SEE ALSO
csh(1), echo(1), ksh(1), sh(1), tr(1), wc(1), attributes(5) NOTES
The -n option is a transition aid for BSD applications, and may not be supported in future releases. SunOS 5.10 3 Aug 1994 echo(1B)
All times are GMT -4. The time now is 04:18 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy