Finding index values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding index values
# 1  
Old 03-25-2011
Finding index values

I have an array XOFFS of size N. I have VDISTMIN and VDISTMAX.
I want to find the beginning and and index in XOFFS such that values are within
[VDISTMIN,VDISTMAX]

Example

Code:
Let XOFFS = (10 20 30 40 50)

Code:
If VDISTMIN = -20 and VDISTMAX = 20
Then ISTR = 1 and IEND = 2 as XOFFS[1] through XOFFS[2] fall within the two distances VDISTMIN and VDISTMAX.

Code:
If VDISTMIN = 20 and VDISTMAX = 80
Then ISTR = 2 and IEND = 5 as XOFFS[2] through XOFFS[5] fall within the two distances VDISTMIN and VDISTMAX.


Last edited by kristinu; 03-25-2011 at 12:24 AM..
# 2  
Old 03-25-2011
Assumptions:
  • Shell ksh or bash
  • Array is sorted ie XOFFS[n] <= XOFFS[n+1]
  • Program takes vdistmin and vdistmax as input arguments 1 and 2
Code:
$ cat ans
#!/bin/bash
XOFFS=( 10 20 30 40 50 )
VDISTMIN=$1
VDISTMAX=$2
ISTR=1
IEND=${#XOFFS[@]}
i=0
while [ $i -lt ${#XOFFS[@]} ]
do
   [ ${XOFFS[i]} -le $VDISTMIN ] && let ISTR=$i+1
   [ ${XOFFS[i]} -le $VDISTMAX ] && let IEND=$i+1
   let i=i+1
done
echo "For $VDISTMIN to $VDISTMAX" ISTR=$ISTR IEND=$IEND
 
$ ./ans -20 20
For -20 to 20 ISTR=1 IEND=2
 
$ ./ans 20 80
For 20 to 80 ISTR=2 IEND=5

# 3  
Old 03-25-2011
Array is sorted. We can have vdistmin and vdistmax as input arguments 1 and 2. It does not matter. I'm still in csh. But I will look at ksh.

---------- Post updated at 11:12 PM ---------- Previous update was at 10:43 PM ----------

I have a problem with IEND
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get index values for multiple matches in the same line with awk?

Hi, I know that echo "bob alice robert alice" | awk '{print index($0,"alice")}' 5Will output the index of the first alice match, is there any way to get the index of all matches?, eg: echo "bob alice robert alice" | awk 'unknown magic' 5:18Thanks for your time. (6 Replies)
Discussion started by: chilicuil
6 Replies

2. Shell Programming and Scripting

check index of a string by finding a letter in it

i would like to search for a letter in a string and get its index position. example: name='john' pos=$(expr index $name o) the result will be equal to 2 (2nd position) how do you make this thing not case sensitive? example: name='john' pos=$(expr index $name O) the... (1 Reply)
Discussion started by: kokoro
1 Replies

3. UNIX for Advanced & Expert Users

Finding causes of slabinfo values

I'm doing a little work on assessing and improving server stability. As part of that, we're reviewing the contents of /proc/slabinfo. We see that sometimes a certain metric-- say, buffer_head-- spike prior to server instability. We're interested in learning what contributes to that as we trace... (0 Replies)
Discussion started by: treesloth
0 Replies

4. Shell Programming and Scripting

Finding change in values

I have an array X = ( -100 -90 -80 -70 -60 -50 -40 30 40 50 60 70 80 90 100 ) I want to find the place where values change from negative to positive. (8 Replies)
Discussion started by: kristinu
8 Replies

5. Programming

FORTRAN -Returning index fir which values fall in a region

I have an 10 element array containing numbers, I want the start and end index in the array for which the values lie between DIST1 and DIST2. It is not working quite right. I also might want a value of 0 if I cannot find an index. V=(/10.0,20.0,30.0,40.0,50.0,60.0,70.0,80.0,90.0,100.0/) ... (4 Replies)
Discussion started by: kristinu
4 Replies

6. Shell Programming and Scripting

Sort from start index and end index in line

Hi All, I have a file (FileNames.txt) which contains the following data in it. $ cat FileNames.txt MYFILE17XXX208Sep191307.csv MYFILE19XXX208Sep192124.csv MYFILE20XXX208Sep192418.csv MYFILE22XXX208Sep193234.csv MYFILE21XXX208Sep193018.csv MYFILE24XXX208Sep194053.csv... (5 Replies)
Discussion started by: krish_indus
5 Replies

7. Shell Programming and Scripting

finding values in between

Hi, I have been trying to find someone with this similar problem but I was out of luck. So I have a file that has two columns that look like this (for example): 10 20 40 50 45 60 90 130 So column 1 is start and column 2 is stop but what I want to do is find whats not represented... (4 Replies)
Discussion started by: phil_heath
4 Replies

8. Programming

random array index returning values not contained

For kicks I wrote up a Password generator after lunch. Let me start with the code: unsigned int x,y,z,c; unsigned int KISS(); unsigned int devrand(); int main( int argc, char** argv ) { int i, j = 1; char pwd = "abcdefghijklmnopqrstuvwxyz" ... (5 Replies)
Discussion started by: VRoemer
5 Replies

9. UNIX for Dummies Questions & Answers

wh inode index starts from 1 unlike array index (0)

brothers why inode index starts from 1 unlike array inex which starts from 0 its a question from the design of unix operating system of maurice j.bach i need to know the answer urgently...someone help please (1 Reply)
Discussion started by: sairamdevotee
1 Replies

10. Filesystems, Disks and Memory

why the inode index of file system starts from 1 unlike array index(0)

why do inode indices starts from 1 unlike array indexes which starts from 0 its a question from "the design of unix operating system" of maurice j bach id be glad if i get to know the answer quickly :) (0 Replies)
Discussion started by: sairamdevotee
0 Replies
Login or Register to Ask a Question