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.
What I need to do with array is to read "N" numbers from keyboard and when
the position of the number is supplied through keyboard that number should be deleted....For doing this I need to store the numbers in an array
Can anyone tell me about the usage of array or any other method to store N numbers
There are exampes for creating and indexing them. You can determine many elements are in an array variable: ${#name[@]}.
One thing to keep in mind is that you might have a variable where name[0] is defined and name[10] is defined but the others aren't set.
So if you have defined a variable "name":
name=(10 11 12 13); name[100]=2
Then the array will have a size of 4.
So bash array variables are sort of a cross between an array and a hash. The element index has to be a positive integer.
You need to use square brackets around the index, and regular brackets around the variable when referencing them:
echo ${name[0]} ${name[1] ${name[100]}
Other general purpose scripting languages such as perl have arrays and hashes.
Arrays in bash are pretty limited. Some other shells (e.g. zsh) have associative arrays, which bash lacks (what a pity!).
If you are starting to require complex data structures, perhaps shell script isn't the best language for your project. If you're going to do a lot of manipulation of strings in arrays and sets and so on, maybe Perl or Python would be a better choice of language. It depends on what you are trying to do.
I agree totally.
you can use arrays in bash and ksh
I've been shell scripting professionally for about 10 years
but I never use them because, by then you should really start thinking about
using a decent scripting language.
they are a lot more fun to use, as matthew said, perl or python maybe.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.