find an available item in array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find an available item in array
# 1  
Old 07-15-2007
find an available item in array

Dear all,

I'm have a sorted array like this:
177
220
1001
2000
2001
2003
2005

notice that 2002 and 2004 are NOT in array.

Then user input a number INPUT, our script should return OUTPUT value like this:
if INPUT is not in array => OUTPUT=INPUT
if INPUT is in array => OUTPUT is the smallest number that larger than INPUT
and should NOT exist in array!


Example with this array:
INPUT => OUTPUT
1000 => 1000
1001 => 1002
2000 => 2002
2005 => 2006

Can anyone help me to complete this on bash shell!
# 2  
Old 07-15-2007
Code:
typeset -i mNbr
typeset -i mInput
typeset -i mCnt
echo 'Enter Number: '
read mInput
mFound='N'
while read mNbr
do
  if [ $mNbr -eq $mInput ]; then
    mCnt=$mNbr
    mFound='Y'
  fi
  if [ "$mFound" = "Y" ]; then
    if [ $mNbr -ne $mCnt ]; then
      echo 'Input = '$mInput' Output = '$mCnt
      exit
    fi
    mCnt=$mCnt+1
  else
    if [ $mNbr -gt $mInput ]; then
      echo 'Input = '$mInput' not found'
      exit
    fi
  fi
done < input_file

# 3  
Old 07-16-2007
Thank you,

It works fine, except the last item!
But it's OK.
# 4  
Old 07-16-2007
Perhaps try something like this, which is at the bash prompt...
Code:
$ for v in 177 220 1001 2000 2001 2003 2005; do a[$v]=1; done
$ read -p "Input = " i; while [[ ${a[$i]} = 1 ]]; do ((i+=1)); done; echo Output = $i
Input = 2000
Output = 2002


Last edited by Ygor; 07-16-2007 at 02:41 AM..
# 5  
Old 07-16-2007
Yah, it's great!
Thank Ygor!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read a lis, find items in a file from the list, change each item

Hello, I have some tab delimited text data, file: final_temp1 aname val NAME;r'(1,) 3.28584 r'(2,)<tab> NAME;r'(3,) 6.13003 NAME;r'(4,) 4.18037 r'(5,)<tab> You can see that the data is incomplete in some cases. There is a trailing tab after the first column for each incomplete row. I... (2 Replies)
Discussion started by: LMHmedchem
2 Replies

2. Shell Programming and Scripting

Bash shell script undefined array item value question

Hello, I'm new here. I test these expressions's value in my script : (in centOS 6 ) #!/bin/bash array='something' echo "############" echo ${array} echo ${array} echo ${array} echo "############" The output result is : ################# something something #################... (5 Replies)
Discussion started by: lingjing
5 Replies

3. Shell Programming and Scripting

Associative Array with more than one item per entry

Hi all I have a problem where i have a large list ( up to 1000 of items) and need to have 2 items pulled from it into variables in a bash script my list is like the following and I could have it as an array or possibly an external text file maintained separately. Every line is different and... (6 Replies)
Discussion started by: kcpoole
6 Replies

4. Shell Programming and Scripting

How to find length of multidimension array ???

Does anyone know how to find length of multi dimension array of following type A Afor simple array I is to do for (i in A)n++ to find length of array but if it is multi dimension how to find the length ? (2 Replies)
Discussion started by: nex_asp
2 Replies

5. Shell Programming and Scripting

Parse find input into array

I need help parsing the output of find into an array. I need to search 3 directories and find all files older than 31 days old. This is what I have so far. TIME=" -maxdepth 1 -mtime +31" DIR1="/dir1/" DIR2="/dir2/" DIR3="/dir3/" FIND_DIR1=$(find ${DIR1}${TIME}) FIND_DIR3=$(find... (8 Replies)
Discussion started by: jrymer
8 Replies

6. Shell Programming and Scripting

Output find to array

Hi I'm trying to write a shell script which finds all the .zip files in a given directory then lists them on the screen and prompts the user to select one by entering a number e.g. The available files are: 1. HaveANiceDay.zip 2. LinuxHelp.zip 3. Arrays.zip Please enter the... (4 Replies)
Discussion started by: zX TheRipper Xz
4 Replies

7. Shell Programming and Scripting

perl - get uniq item from an array?

practicing perl now and hope to get uniq item from an array: my current work: #!/usr/local/bin/perl my @source = ("aaa", "aaa", "bbb", "ccc", "ddd"); my $index=0; my @uniq; foreach (@source) { chomp; # push first item to @uniq if ($index == 0) { push @uniq, $_; ... (2 Replies)
Discussion started by: tiger2000
2 Replies

8. Shell Programming and Scripting

Using awk to find sum of an array

Hi I am really new to awk and using shell script but I am wondering if its possible to find the sum of an array? I looked online but most of the things there are confusing, and when I tried it on my own it kept giving me the value of the last entry into the array for the sum. I have an array... (2 Replies)
Discussion started by: razrnaga
2 Replies

9. Shell Programming and Scripting

Bash: Find and echo value in Array

Newbie to bash here. I think this is fairly simple, but I have searched and cannot figure it out. In the code below, I am searching an array for an IP address, and then printing the IP address if found. However, I would like to print the actual variable found such as 2.2.2.2=2, but cannot figure... (1 Reply)
Discussion started by: lozwell
1 Replies

10. Shell Programming and Scripting

problem with array=($(find ....)

hi, I get a *.dat files list in an array using: array=($(find . -name "*.dat")) the problem is that when a filename contains spaces, each space-separated token of the filename is in a different element of array. For instance if I have: x@x:~/tmp$ ls *.dat test1.dat test 2.dat ... (1 Reply)
Discussion started by: jul
1 Replies
Login or Register to Ask a Question