Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
| 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
12-22-2008, 08:00 AM
|
#1
|
|
Member
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 517
Rep:
|
Bash Variable Array, Trying to add another value into the array
Hi All,
Just thinking about a variable array and i'd like to if possible...
when working with an array simply add a value to the array at the next available slot/number so to speak.
IE
i have an array:-
Code:
array=( `echo 1 2 3 4` )
this obviously giving me 4 values in my array
Code:
echo ${#array[@]}
4
Instead of then having to do something like:-
Code:
number=`echo ${#array[@]}`
((number++)) #add one
array[${number}]=5
Is it possible to not have to know the next number in the array so the value can just be added to the next available slot in the array so to speak. ie
Code:
array[next_availble_number]=5
rather than going through the rigmarole of all that above. It would be much nicer if its possible to just say chuck this value anywhere in the array as i don't care where it goes. Just as long as its not overwriting something already in the array, and is using the next available number in the array so that when you do something like:-
or
you still get returned the correct numbers of 5 and 1 2 3 4 5
Any ideas?
Cheers,
MJ
|
|
|
|
12-22-2008, 04:53 PM
|
#2
|
|
Moderator
Registered: May 2001
Posts: 24,786
|
You could use 'array[$[${#array[@]}+1]]=Whatever'?
|
|
|
|
12-22-2008, 05:06 PM
|
#3
|
|
Member
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 517
Original Poster
Rep:
|
Hey unspawn,
Thanks very much, looks perfect. I think i need to look into more details with bash and all the [] {} () (()) and how they all work.
Thanks though, i can see that should work fine.
Cheers,
MJ
|
|
|
|
03-02-2009, 07:33 AM
|
#4
|
|
LQ Newbie
Registered: Dec 2005
Posts: 4
Rep:
|
Dear all,
is it possible to add a constant integer to all array elements
in one go.
e.g.
I have and array:
Code:
myarray=( 2 3 2 14 6 32 5 455 23 )
On completing the operation, I want the array to be modified as:
Code:
myarray=( 4 5 4 16 8 34 7 457 25 )
Just as one is able to substitute all strings in an array
in bash at one go, how do I do the above in one go?
Thanks!
--kvaibhav
|
|
|
|
03-02-2009, 07:50 AM
|
#5
|
|
Member
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 517
Original Poster
Rep:
|
Hello There,
Not sure if this is the best way but a quick go at it from my point of view and i come up with:-
Code:
# myarray=( 4 5 4 16 8 34 7 457 25 )
# count=0
# for i in ${myarray[@]}; do myarray[$count]=`expr $i + 2`; ((count++));done
# echo ${myarray[@]}
6 7 6 18 10 36 9 459 27
Just change the "2" in the `expr $i + 2` to be what ever amount you want to be added to each number in the variable.
Hope this helps,
MJ
Last edited by helptonewbie; 03-02-2009 at 07:52 AM.
|
|
|
|
03-02-2009, 07:55 AM
|
#6
|
|
Senior Member
Registered: Jul 2004
Location: Nantes (France)
Distribution: Arch Linux
Posts: 1,897
Rep:
|
Array indexes are zero-based. So you don't need to compute anything. Example:
Code:
[yves@localhost ~]$ array=( 1 2 3 4 )
[yves@localhost ~]$ echo ${#array[*]}
4
[yves@localhost ~]$ array[${#array[*]}]=5
[yves@localhost ~]$ array[${#array[*]}]=6
[yves@localhost ~]$ for ((i=0; i<${#array[*]}; i++)); do echo "array[$i] = ${array[$i]}"; done
array[0] = 1
array[1] = 2
array[2] = 3
array[3] = 4
array[4] = 5
array[5] = 6
Yves.
[Edit]@kvaibhav:
Code:
[yves@localhost ~]$ for ((i=0; i<${#array[*]}; i++)); do array2[$i]=$((${array[$i]} + 2)); done
Last edited by theYinYeti; 03-02-2009 at 08:06 AM.
|
|
|
|
03-02-2009, 11:18 PM
|
#7
|
|
LQ Newbie
Registered: Dec 2005
Posts: 4
Rep:
|
Dear all,
thanks for the reply.
The solution IS evident and thanks for it.
What I was looking for, was an 'array operation' that does the same.
For example, in the following:
Code:
# Replace all occurrences of substring.
echo ${arrayZ[@]//iv/YY} # one two three four fYYe fYYe
# Applied to all elements of the array.
(Ref: http://tldp.org/LDP/abs/html/arrays.html)
all elements of array get altered on just one array operation.
I was looking for such a solution.
Thanks!
--kvaibhav
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 09:46 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|