If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > popping element from an array

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-24-06, 01:57
bhaizone bhaizone is offline
Registered User
 
Join Date: Feb 2006
Posts: 31
popping element from an array

How do I pop out an element from anywhere in an array and then the corresponding elements organise themselves accordingly?(working on korn shell)

eg. set -A array 11 12 13 14 15
Now I want to remove 13, then I get
echo ${array[@]} = 11 12 14 15
Reply With Quote
  #2 (permalink)  
Old 02-27-06, 19:33
Bob4480 Bob4480 is offline
Registered User
 
Join Date: Feb 2004
Location: Los Angeles, CA
Posts: 28
Quote:
Originally Posted by bhaizone
How do I pop out an element from anywhere in an array and then the corresponding elements organise themselves accordingly?(working on korn shell)

eg. set -A array 11 12 13 14 15
Now I want to remove 13, then I get
echo ${array[@]} = 11 12 14 15
Set the element you want to eliminate to null. (Remember Arrays start with 0 (zero))

Code:
 
set -A MY_ARRAY 11 12 13 14 15
echo ${MY_ARRAY[@]}
11 12 13 14 15
 
MY_ARRAY[2]=''
echo ${MY_ARRAY[@]}
11 12 14 15
Reply With Quote
  #3 (permalink)  
Old 02-27-06, 19:49
Bob4480 Bob4480 is offline
Registered User
 
Join Date: Feb 2004
Location: Los Angeles, CA
Posts: 28
One thing to note. Although it displays properly, the array variable is actually null. There fore you still have 5 elements in the array. If this is a problem you can reset the entire array like this:

Code:
$ set -A MY_ARRAY 11 12 13 14 15
$ echo ${MY_ARRAY[@]}
11 12 13 14 15
$
$  MY_ARRAY[2]=''
$ echo ${MY_ARRAY[@]}  #Array still has 5 elements. Just one is null.
11 12 14 15
$
$ echo ${MY_ARRAY[2]}  #This is now Null.
 
$
$ set -A MY_ARRAY `echo ${MY_ARRAY[@]}`  #Reset Array to 4 elements
$ echo ${MY_ARRAY[2]}  
14
$
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On