LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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, 08: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, 08:14 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 12,154

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
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, 08: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, 12:09 PM   #4
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Lubuntu
Posts: 19,176
Blog Entries: 4

Rep: Reputation: 430Reputation: 430Reputation: 430Reputation: 430Reputation: 430
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, 12:23 PM   #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, 01:45 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
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, 09:25 PM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
Is there an issue with using unset?
Code:
unset array[3]
 
Old 02-08-2011, 12:30 PM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
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 12:31 PM. Reason: speeling
 
Old 02-08-2011, 01:41 PM   #9
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943
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 08:13 AM.
 
Old 02-08-2011, 04:22 PM   #10
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Thanks Nominal Animal

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

Original Poster
Rep: Reputation: 0
Thumbs up

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

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
Please mark as SOLVED once you have a solution
 
Old 10-20-2013, 03: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, 05:25 PM   #14
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 02:49 PM
[SOLVED] shell script help: copying directory list into an array and then accessing the array richman1234 Linux - Newbie 6 07-26-2010 12:19 AM
Array shell script pooppp Linux - Networking 6 08-01-2008 09:37 AM
Help!! Shell script to get output of ls -l command into an array kasthana Programming 8 06-02-2008 12:37 AM
shell script array problem rche3252 Programming 1 10-09-2003 12:43 AM


All times are GMT -5. The time now is 07:17 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration