ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
1. how do i create an array with 5 entries
2. output one of the entries in the array randomly
3. subtract the entry output from the array
4. repeat steps 2 and 3 until there are no more entries in the array
Per the LQ Rules, please do not post homework assignments verbatim. We're happy to assist if you have specific questions or have hit a stumbling point, however. Let us know what you've already tried and what references you have used (including class notes, books, and Google searches) and we'll do our best to help. Also, keep in mind that your instructor might also be an LQ member.
it is not for homework. i posted in that format hoping to eliminate confusion about my post. what i am trying to do is use an array to replace a long series of if statements. if i have 50 foreign words i want to learn, i have to write 50 if statements, if i guess right do this, if i guess wrong do that. I was wondering if i could use an array in a shell script in place of an if statement that would compare the read input to the output in the array and remove entries in the array until it is empty. i was hoping i could use one if statement.
Useful links but they don't say how to remove elements from the array and bash does not have that ability so you will need to replace the used element with the last element and randomly pick from a shorter set of elements each time. Something like (untested) ...
Code:
array=( a b c d e)
for (( idx_max=5; idx_max > 0; idx_max-- ))
do
idx=$(( RANDOM % idx_max ))
echo "Random value is ${array[idx]}"
array[idx]=array[idx_max]
done
I think so because there would be no easy way of choosing an array element at random which was not unset; there would be an infinitesimally chance of it taking forever to do so!
Last edited by catkin; 02-08-2011 at 11:31 AM.
Reason: speeling
I frequently use Google and the like to get questions answered and I hit upon these forums more times than anything else. PLEASE PLEASE PLEASE just answer the question. All I hear is mocking of the original question. People like me will come with the same question will see these and when there is petty things like "is this home work" SO WHAT one persons home work is anothers real life question. I am an old Mainframe COBOL programmer thrust into Unix Admin and know nothing about it so I try to use these forums, but they are useless when we have "answers" like this one.
SO... since you did NOT answer the simple question above, I will have to look elsewhere.
Actually, if you had taken the time to read the replies above you would have noticed that parts of the question have been answered and once combined you could come up with your own solution.
This of course assumes that you are trying to learn and not just have the work done for you.
As to not doing homework, like most forums this one has a set of guide lines and if you are not inclined to think they are for you, you are welcome to go elsewhere.
I can tell you that with an initial post like that you are unlikely to get much help if any with an attitude like that.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.