queue and stack using array

Home -> Programming -> Shell script -> Bourne shells

13471 views

From the computer of: hpx (3 recipes)
Created: Jun 08, 2005


Add a comment

Add to:
Add to stumbleuponAdd to del.icio.usDigg itAdd to FURL

here is a series of operation on array, we can use these functions to implement a queue or stack that can help us more

push:
array=("${array[@]}" $new_element)

pop:
array=(${array[@]:0:$((${#array[@]}-1))})

shift:
array=(${array[@]:1})

unshift
array=($new_element "${array[@]}")

function del_array {
local i
for (( i = 0 ; i < ${#array[@]} ; i++ ))
do
if [ "$1" = "${array[$i]}" ] ;then
break
fi
done
del_array_index $i
}


function del_array_index {
array=(${array[@]:0:$1} ${array[@]:$(($1 + 1))})
}

Subscribe to the Tech-Recipes Newsletter

You can get tips like this delivered in your email every week!

Enter your Email

We will never, ever sell your email address or spam you.





Related recipes:

  bash shell script iterate through array values
  bash array operations
  bash shell script declaring/creating arrays
  bash shell script accessing array variables
  Bourne/bash shell script: while loop syntax
  simple function for print colorful text
  Determine if a file is writable by a Bourne script user
  Hide password entry in Bourne/bash shell script
  Determine if file exists in a Bourne/bash shell script
  Bourne/bash shell script for loop syntax

 

Sponsored links

 

Login

Nickname

Password

Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.