Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2006
    Posts
    31

    Unanswered: 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

  2. #2
    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

  3. #3
    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
    $

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •