The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-21-2008
msb65 msb65 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 89
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
  #2 (permalink)  
Old 10-21-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,808
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

  #3 (permalink)  
Old 10-21-2008
msb65 msb65 is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 89
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
  #4 (permalink)  
Old 10-21-2008
Lakris Lakris is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 246
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:08 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0