LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 02-07-2011, 07:10 AM   #1
dnoob
LQ Newbie
 
Registered: Feb 2011
Posts: 17

Rep: Reputation: 0
Create Array in Shell Script


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

please help
 
Old 02-07-2011, 07:14 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
Welcome to LinuxQuestions.

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.
 
1 members found this post helpful.
Old 02-07-2011, 07:34 AM   #3
dnoob
LQ Newbie
 
Registered: Feb 2011
Posts: 17

Original Poster
Rep: Reputation: 0
Create Array in Shell script

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.
 
Old 02-07-2011, 11:09 AM   #4
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 02-07-2011, 11:23 AM   #5
goossen
Member
 
Registered: May 2006
Location: Bayern, Germany
Distribution: Many
Posts: 224

Rep: Reputation: 41
You will find these links useful:

Advanced Bash-Scripting Guide: Chapter 27. Arrays
and
Advanced Bash-Scripting Guide: Example 9-12. Picking a random card from a deck
 
Old 02-07-2011, 12:45 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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
 
Old 02-07-2011, 08:25 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Is there an issue with using unset?
Code:
unset array[3]
 
Old 02-08-2011, 11:30 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by grail View Post
Is there an issue with using unset?
Code:
unset array[3]
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
 
Old 02-08-2011, 12:41 PM   #9
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
grail and catkin are both right. You need to reindex the array after deleting an element. Consider this:
Code:
#!/bin/bash
words=("first", "second", "the third", "fourth")
while [ ${#words[@]} -gt 0 ]; do
    i=$[RANDOM % ${#words[@]}]

    echo "${words[i]}"

    unset words[i]
    words=("${words[@]}")
done
Nominal Animal

Last edited by Nominal Animal; 03-21-2011 at 07:13 AM.
 
Old 02-08-2011, 03:22 PM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Thanks Nominal Animal

That re-indexing technique is neat.
 
Old 02-08-2011, 04:05 PM   #11
dnoob
LQ Newbie
 
Registered: Feb 2011
Posts: 17

Original Poster
Rep: Reputation: 0
Thumbs up

thanks alot !!
 
Old 02-08-2011, 07:45 PM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Please mark as SOLVED once you have a solution
 
Old 10-20-2013, 02:02 PM   #13
rrainbolt
LQ Newbie
 
Registered: Oct 2013
Posts: 2

Rep: Reputation: Disabled
JUST ANSWER THE QUESTION for all of us

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.
 
Old 10-20-2013, 04:25 PM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
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.
 
  


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
array in shell script Abid Malik Programming 2 10-16-2010 01:49 PM
[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
Array shell script pooppp Linux - Networking 6 08-01-2008 08:37 AM
Help!! Shell script to get output of ls -l command into an array kasthana Programming 8 06-01-2008 11:37 PM
shell script array problem rche3252 Programming 1 10-08-2003 11:43 PM

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

All times are GMT -5. The time now is 06:21 PM.

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