![]() |
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !! |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| eval, array, number of elements - couldn't get it | alex_5161 | Shell Programming and Scripting | 1 | 07-01-2008 04:24 PM |
| compare a variable against array elements | fearboy | Shell Programming and Scripting | 2 | 06-27-2008 06:45 PM |
| Rearrange array elements-Pls help | tj23 | Shell Programming and Scripting | 4 | 06-20-2008 10:29 AM |
| Perl - New line in array elements | bperl | Shell Programming and Scripting | 4 | 04-03-2008 10:56 PM |
| To return the elements of array | Sudhakar333 | Shell Programming and Scripting | 5 | 08-06-2007 03:20 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
Deleting Array Elements
Hi,
I am writing a BASH shell script. I have an array that will contain IN ANY ORDER the following elements: DAY 8D MO NS. I would like to erase the element DAY, but since the order of the elements in the array are random, I will not know which element # DAY is (ie it's not as simple as saying unset array[#], as I won't know what # is). How can I do this? Thanks, Mike |
|
||||
|
By "DAY" do you mean literally "DAY" or is it something like "Sunday" or "Mon" or "MON"? -- change the following code to meet your needs Code:
days="SunMonTuesWedThuFriSat"
cnt=0
while [ $cnt -lt ${#array ] ;
do
echo "$days" | grep -q "${array[cnt]}"
if [ $? -eq 0 ] ; then
unset ${array[cnt]}
break
fi
done
|
|
||||
|
Hi and thanks for the reply.
I don't think I did a good enough job explaining myself. I have the following array: rrdhcp78-122:L0 msb65$ test_array=(8D DAY MO) rrdhcp78-122:L0 msb65$ echo ${test_array[@]} 8D DAY MO I understand that if I wanted to remove the element "DAY" from "test_array" I would type: "unset $test_array[1]". However, in my script the elements of "test_array" will be in a random order. Therefore "DAY" won't always be the second element. Given a random "test_array", how do I determine which element number (#) "DAY" corresponds to so that I can remove it: unset $test_array[#]? Mike |
|
||||
|
Hi, As Jim stated You can loop over You array and unset the element when You hit it with a certain criterion, here's another example: Code:
#!/bin/bash
arr=(qwe xc 3 DAY grok mok)
crit=DAY
cnt=0
for x in ${arr[@]}; do
[ $x = $crit ] && unset arr[cnt] && break || ((cnt++))
done
echo ${arr[@]}
(I am using not so very portable bashism here...) /Lakris |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|