Using unset to delete array elements


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using unset to delete array elements
# 1  
Old 10-22-2008
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
# 2  
Old 10-22-2008
The elements are not reordered.
Code:
for i in 0 1 2 3 
do
   echo "test_array  $i  = ${test_array[i]}"
done

# 3  
Old 10-22-2008
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 10-22-2008
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
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Get unique elements from Array

I have an array code and output is below: echo $1 while read -r fline; do echo "%%%%%%$fline%%%%%" fmy_array+=("$fline") done <<< "$1" Output: CR30903 YU0007 SRIL CR30903 Yogesh SRIL %%%%%%CR30903 YU0007 SRIL%%%%% %%%%%%CR30903 Yogesh SRIL%%%%% ... (8 Replies)
Discussion started by: mohtashims
8 Replies

2. UNIX for Beginners Questions & Answers

Unset array element and save to file in Bash

#!/bin/bash X=(2H 4S 10D QC JD 9H 8S) How do I unset the 10D from this array and save it to a file? Please use CODE tags as required by forum rules! (5 Replies)
Discussion started by: cogiz
5 Replies

3. Shell Programming and Scripting

Help reading the array and sum of the array elements

Hi All, need help with reading the array and sum of the array elements. given an array of integers of size N . You need to print the sum of the elements in the array, keeping in mind that some of those integers may be quite large. Input Format The first line of the input consists of an... (1 Reply)
Discussion started by: nishantrefound
1 Replies

4. Shell Programming and Scripting

Random elements from array

Hi I wanted to print random elements from an array at bash shell I use the following code, but I always see first element getting printed #!/bin/bash c=1 expressions=(pink red white yellow purple) while ]; do echo "The value of RANDOM is $RANDOM" selectedexpression=${expressions}]};... (5 Replies)
Discussion started by: Priya Amaresh
5 Replies

5. Programming

Array Elements Check

Hi everyone, :) I'm trying to make a simple C program that scans an array of chars to see if its elements are similar. I can't understand what's wrong. Could you help me to fix this? Here is the code. Thanks! #include<stdio.h> int main() { int arr; int i, len; int flag =... (10 Replies)
Discussion started by: IgorGest
10 Replies

6. Shell Programming and Scripting

Grouping array elements - possible?

I have a script which takes backup of some configuration files on my server. It does that by using an array which contains the complete path to the files to backup. It copys the files to a pre defined dir. Each "program" has it's own folder, ex. apache.conf is being copied to /predefined... (7 Replies)
Discussion started by: dnn
7 Replies

7. UNIX for Dummies Questions & Answers

printing array elements

Is there a way to print multiple array elements without iterating through the array using bash? Can you do something like... echo ${array}and get all those separate elements from the array? (2 Replies)
Discussion started by: jrymer
2 Replies

8. UNIX for Dummies Questions & Answers

Help with replacing Array elements

Hi, I have an array containing following sample information @array = qw (chr02 chr02 chr02 chr02 chr02 chr03 chr03 chr04 chr04 chr05 chr05 chr05 chr07 chr07) I need to replace all duplicate entries by an underscore to get the following output@array = qw (chr02 _ _ _ _ chr03 _ chr04 _ chr05 _ _... (4 Replies)
Discussion started by: pawannoel
4 Replies

9. Shell Programming and Scripting

Array with String Elements

How can I get my array to understand the double-quotes I'm passing into it are to separate text strings and not part of an element? here's what I'm working with... db2 -v connect to foo db2 -x "select '\"' || stats_command || '\",' from db2law1.parallel_runstats where tabname = 'BAZ'" set... (4 Replies)
Discussion started by: djschmitt
4 Replies

10. Shell Programming and Scripting

PHP: how can I delete empty/NULL elements from a multi-dimensional array.

Hi all I have a file that i'm running and exec(cat ./dat) against..and putting its contents into any array, then doing an exploding the array into a multi-dimension array... The 15 multi-dimensional arrays have elements that are null/empty, I would like to remove/unset these elements and then... (2 Replies)
Discussion started by: zeekblack
2 Replies
Login or Register to Ask a Question