LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 04-24-2011, 12:41 AM   #1
New2LQ
LQ Newbie
 
Registered: Feb 2011
Posts: 5

Rep: Reputation: 0
Array stuck


Hi everyone. I need help with an array; it continuously loads the first value of a variable. The variable is composed of 4 digits; also, there no comas or spaces separating the digits.
Here's all the code, including output/echoed:
Code:
#Testing formatting input for mm function

FLOOR=1000   #bottom limit of secret number
CEILING=9999 #upper limit of secret number

secret=0   #initializes number
while [ "$secret" -le $FLOOR ]
         #obtaining bottom range
do
  secret=$RANDOM
  let "secret %= $CEILING"
     #number now 4 digits long
done

echo "$secret"

#Placing the number in sn_array

declare -a sn_array  #defines sn_array[ ]

let si=0    #index for sn_array

while [ "$si" -le 3 ]
    do
      sn_array[$si]=${secret:$si:1} 	 	
 	 #control element's location
      ((si +=1))
      	 #increasing index and location
    done

echo $sn_array[0]
echo $sn_array[1]
echo $sn_array[2]
echo $sn_array[3]

output of last run:
5137
5[0]
5[1]
5[2]
5[3]
Thanks in advance.
 
Old 04-24-2011, 02:59 AM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Try this "bash" syntax instead:
Code:
sn_array[0]="A"
sn_array[1]="B"
sn_array[2]="C"
echo sn_array[0]: ${sn_array[0]}
echo sn_array[1]: ${sn_array[1]}
echo sn_array[2]: ${sn_array[2]}
Quote:
sn_array[0]: A
sn_array[1]: B
sn_array[2]: C
This is a good link:
http://www.linuxjournal.com/content/bash-arrays

'Hope that helps .. PSM

Last edited by paulsm4; 04-24-2011 at 03:00 AM. Reason: Added sample output
 
Old 04-24-2011, 05:48 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I am curious why you use (()) in one part of the code but then use let and things like -le in other parts of the code?

For example:
Code:
FLOOR=1000   #bottom limit of secret number
CEILING=9999 #upper limit of secret number

# this is not required secret=0   #initializes number
while (( secret < FLOOR ))
         #obtaining bottom range
do
  secret=$RANDOM
  (( secret %= CEILING ))
     #number now 4 digits long
done

echo "$secret"

#Placing the number in sn_array

declare -a sn_array  #defines sn_array[ ]

for (( si = 0; si <= 3; si++ ))
do
      sn_array[si]=${secret:$si:1} 	 	
done

echo ${sn_array[0]}
echo ${sn_array[1]}
echo ${sn_array[2]}
echo ${sn_array[3]}
Mainly the only issue you had was missing the curly braces around the echoed items
 
Old 04-25-2011, 04:02 AM   #4
New2LQ
LQ Newbie
 
Registered: Feb 2011
Posts: 5

Original Poster
Rep: Reputation: 0
Thank you paulsm4 and grail, both of your suggestions were invaluable to the solution! Paul, your suggestion of: sn_array[0]=”A” {etc.} was confusing since “A” wasn’t defined. In incorporated your echo suggestion in the loop, I discovered through the error message that the correct digits were loading into the right spot…great job on that! Next, Grail, the reason for the test brackets was I thought they had to be there…I’m very new with Linux and bash. But your suggestion made my code cleaner! As for the solution…well I had to use n as a variable for the length rather than the digit 1...and off it went.
Thanks to you two, my code not only works, but it’s cleaner.
Thanks from a grateful newbie! Oh, and here’s the working code:
Quote:
#Testing formatting input for mm
FLOOR=1000 #bottom limit of secret number
CEILING=9999 #upper limit of secret number

while ((secret < FLOOR)) #obtaining bottom range
do
secret=$RANDOM
((secret %= CEILING)) #number is now 4 digits long
done

declare -a sn_array #defines sn_array[ ]

for ((si = 0; si <= 3; si++ ))
do
n=1
sn_array[si]=${secret:$si:$n}
done
echo $secret
echo ${sn_array[@]}
Output of last run:
3957
3 9 5 7

Last edited by New2LQ; 04-25-2011 at 06:54 AM.
 
Old 04-25-2011, 04:48 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Well here as an alternative for you to mull over
Code:
#Testing formatting input for mm
FLOOR=1000 #bottom limit of secret number
CEILING=9999 #upper limit of secret number

while (( (secret = RANDOM % CEILING ) < FLOOR ));do :;done

declare -a sn_array #defines sn_array[ ]

sn_array=($( echo $secret | awk -F "" '$1=$1' ))

echo $secret
echo ${sn_array[@]}
 
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] Bash: Calculating in array and storing result in array ghantauke Linux - Newbie 6 12-02-2010 12:28 PM
Server array (mysql, php) -> gboolean array in c client kalleanka Programming 1 07-27-2010 06:50 AM
[SOLVED] shell script help: copying directory list into an array and then accessing the array richman1234 Linux - Newbie 6 07-25-2010 11:19 PM
[bash] indirect array reference to array with values containing spaces Meson Linux - Software 9 06-04-2010 09:38 PM
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 10:07 AM

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

All times are GMT -5. The time now is 11:40 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