LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-13-2013, 02:54 PM   #1
Garrett85
Member
 
Registered: Jan 2011
Posts: 332

Rep: Reputation: 6
BASH - sting concatenation


In the script I'M writting below I can't get STR hold a string, it always does an output as empty.

Code:
#! /bin/bash -x

# Program to take a user numerical input number and return it as a sentence.
#
#
#
### ARRAYS ###
ARRAY1=("" "one" "two" "three" "four" "five" "six" "seven" "eight" "nine")

ARRAY2=("" "eleven" "twelve" "thirteen" "fourteen" "fifteen" "sixteen" "seventeen" "eightteen" "nineteen")

ARRAY3=("" "ten" "tweenty" "thirty" "forty" "fifty" "sixty" "seventy" "eighty" "ninety")

ARRAY4=("" "" "thoudand" "million" "billion")

##############################
### FUNCTION-leading_zeros ###
##############################

leading_zeros() {
	local NUM=${1}
	local NUMBEROFDIGITS=${2}
	local NUMBEROFGROUPS=${3}

	while [[ $(( ${#NUM} / 3 )) -ne ${NUMBEROFGROUPS} ]] || [[ ${#NUM} -lt 3  ]]; do
		NUM="0${NUM}"
	done

	echo ${NUM}
}

read -p "Enter a number no higher than the billions range" NUM

NUMBEROFDIGITS=${#NUM}
NUMBEROFGROUPS=$(( NUMBEROFDIGITS / 3 ))

if [[ ${NUMBEROFDIGITS} -lt 3 ]]; then # {
	NUMBEROFGROUPS=1
	NUM=$( leading_zeros ${NUM} ${NUMBEROFDIGITS} ${NUMBEROFGROUPS})
elif [[ $(( NUMBEROFDIGITS % 3 )) -ne 0 ]]; then # } {
	let NUMBEROFGROUPS=NUMBEROFGROUPS+1
	NUM=$( leading_zeros ${NUM} ${NUMBEROFDIGITS} ${NUMBEROFGROUPS})
fi # }

STR=""
STARTPOINT=0
while [[ ${NUMBEROFGROUPS} -gt 0 ]]; do # {
	CURRENTGROUP=${NUM:${STARTPOINT}:3}
	D1=${CURRENTGROUP:0:1}
	D2=${CURRENTGROUP:1:1}
	D3=${CURRENTGROUP:2:1}

	if [[${D1} -eq 0 ]]; then # {
		if [[ ${D2} -eq 0 ]] && [[ ${D3} -ne 0 ]]; then # {
			STR="${STR} ${ARRAY1[D3]} ${ARRAY4[NUMBEROFGROUPS]}"

		fi # }
	fi # }
	echo "${D1} ${D2} $D3}"
	let STARTPOINT=STARTPOINT+3
	let NUMBEROFGROUPS=NUMBEROFGROUPS-1
done # }

echo ${NUM}

echo "The string is ${STR}"
 
Old 01-13-2013, 04:46 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
There are a couple of typos but it seems to work logically exactly as specified, I've added comments to clarify:

Code:
...
#kbp: make STR an empty string
STR=""
STARTPOINT=0
while [[ ${NUMBEROFGROUPS} -gt 0 ]]; do # {
    CURRENTGROUP=${NUM:${STARTPOINT}:3}
    D1=${CURRENTGROUP:0:1}
    D2=${CURRENTGROUP:1:1}
    D3=${CURRENTGROUP:2:1}

    #kbp: if the number entered has a leading zero...
    if [[ ${D1} -eq 0 ]]; then # {
        #kbp: .. and if the second digit is a zero and the the third digit isn't ...
        if [[ ${D2} -eq 0 ]] && [[ ${D3} -ne 0 ]]; then
            #kbp: ... set STR
            STR="${STR} ${ARRAY1[D3]} ${ARRAY4[NUMBEROFGROUPS]}"
        fi
    fi
    echo "${D1} ${D2} $D3"
STR will only be set to a non-empty string if the number entered is in the format: 00[1-9] .. is this what you wanted?
 
Old 01-13-2013, 07:33 PM   #3
Garrett85
Member
 
Registered: Jan 2011
Posts: 332

Original Poster
Rep: Reputation: 6
How

Why does it need to be in the format 00[1-9]? Each part of the three digit numbe is broken down into it's own variable D#. Why is STR not getting the string values from the ARRAYs?

Quote:
Originally Posted by kbp View Post
There are a couple of typos but it seems to work logically exactly as specified, I've added comments to clarify:

Code:
...
#kbp: make STR an empty string
STR=""
STARTPOINT=0
while [[ ${NUMBEROFGROUPS} -gt 0 ]]; do # {
    CURRENTGROUP=${NUM:${STARTPOINT}:3}
    D1=${CURRENTGROUP:0:1}
    D2=${CURRENTGROUP:1:1}
    D3=${CURRENTGROUP:2:1}

    #kbp: if the number entered has a leading zero...
    if [[ ${D1} -eq 0 ]]; then # {
        #kbp: .. and if the second digit is a zero and the the third digit isn't ...
        if [[ ${D2} -eq 0 ]] && [[ ${D3} -ne 0 ]]; then
            #kbp: ... set STR
            STR="${STR} ${ARRAY1[D3]} ${ARRAY4[NUMBEROFGROUPS]}"
        fi
    fi
    echo "${D1} ${D2} $D3"
STR will only be set to a non-empty string if the number entered is in the format: 00[1-9] .. is this what you wanted?
 
Old 01-13-2013, 07:48 PM   #4
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
Because those are the conditions imposed by the 'if' loops you wrote. Let me write your logic into an english sentence:

If the first digit is zero then check if the second digit is zero and that the third digit is not zero - if all of these tests are true then make STR equal to the concatenation of what STR is now and ARRAY1[D3] and ARRAY4[NUMBEROFGROUPS].

If any of the tests fail then we drop out without setting STR .. got it ?

<edit>This is only the case for 3 digit numbers...</edit>

Last edited by kbp; 01-13-2013 at 07:54 PM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] extraction of sting xerox Linux - Newbie 4 07-29-2011 06:02 AM
[bash] sting as variable dzaku Programming 6 06-27-2011 09:31 AM
Variable concatenation in bash. stf92 Linux - Newbie 3 11-11-2010 12:22 AM
Bash Concatenation alieas Programming 3 09-04-2006 01:45 PM
Sting.h alaios Programming 2 08-21-2005 04:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration