Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Using unset to delete array elements

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-22-2008
msb65 msb65 is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 12 December 2011, 2:18 PM EST
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Using unset to delete array elements

Hi,

I am writing a BASH script. My questions regard deleting elements of arrays.

I have an array:
michael-browns-powerbook-g4-15:~ msb65$ test_array=(1 2 3 4)
michael-browns-powerbook-g4-15:~ msb65$ echo ${test_array[@]}
1 2 3 4

To delete the second element of test_array I type:
michael-browns-powerbook-g4-15:~ msb65$ unset test_array[1]
michael-browns-powerbook-g4-15:~ msb65$ echo ${test_array[@]}
1 3 4

I would like to again delete the second element (which is now 3). So I repeat the previous two lines of code:
michael-browns-powerbook-g4-15:~ msb65$ unset test_array[1]
michael-browns-powerbook-g4-15:~ msb65$ echo ${test_array[@]}
1 3 4

- Why is it that the second element was not deleted? Am I not deleting array elements properly?

Thanks.
Mike
Sponsored Links
    #2  
Old Unix and Linux 10-22-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 25 April 2015, 8:53 AM EDT
Location: NM
Posts: 10,434
Thanks: 328
Thanked 857 Times in 796 Posts
The elements are not reordered.

Code:
for i in 0 1 2 3 
do
   echo "test_array  $i  = ${test_array[i]}"
done

Sponsored Links
    #3  
Old Unix and Linux 10-22-2008
msb65 msb65 is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 12 December 2011, 2:18 PM EST
Posts: 121
Thanks: 0
Thanked 0 Times in 0 Posts
Hi Jim,

I really appreciate your help! Two final questions:

- When you use unset, you refer to the array with "$" and "{}". When I use them I get an error:
michael-browns-powerbook-g4-15:~ msb65$ array=(1 2 3 4)
michael-browns-powerbook-g4-15:~ msb65$ unset ${array[1]}
-bash: unset: `2': not a valid identifier

The only way I get it work is if I type:
michael-browns-powerbook-g4-15:~ msb65$ unset array[1]

Why is that?. What is the difference between the two methods?

- Is there any way to just eliminate an array element (ie reorder the array)?

Mike
    #4  
Old Unix and Linux 10-22-2008
fpmurphy's Unix or Linux Image
fpmurphy fpmurphy is offline Forum Staff  
who?
 
Join Date: Dec 2003
Last Activity: 24 April 2015, 9:36 PM EDT
Location: /dev/ph
Posts: 4,914
Thanks: 68
Thanked 453 Times in 421 Posts
One way ....

Code:
$ test=(1 2 3 4)
$ echo ${test[@]}
1 2 3 4
$ test=( ${test[@]:0:1} ${test[@]:2:2})
$ echo ${test[@]}
1 3 4
$ echo ${test[1]}
3
$

Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Unix or Linux Image More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
csh array missing some elements tkirk Shell Programming and Scripting 1 04-21-2011 11:05 PM
PHP: how can I delete empty/NULL elements from a multi-dimensional array. zeekblack Shell Programming and Scripting 2 12-05-2009 11:03 PM
awk - array elements as condition shellwell Shell Programming and Scripting 1 07-02-2009 04:49 AM
Accessing array elements mansa Shell Programming and Scripting 1 10-30-2008 06:28 AM
Deleting Array Elements msb65 UNIX for Dummies Questions & Answers 3 10-21-2008 02:29 PM



All times are GMT -4. The time now is 11:58 AM.