LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-05-2010, 05:21 AM   #1
himu3118
Member
 
Registered: Mar 2010
Posts: 35

Rep: Reputation: 15
problem in handling arrays in bash


I want to declare an array with fixed size and initialize all the elements to zero. i want all elements of x to be zero and i dont know how to specify the size of array.
also i have problem in comparing values in loops.

my script is as follows:

i=0
let k=0
declare -a x

for U in ${S[@]}
do
for (( T=1276082023 ; $T -le 1276082139 ; (( T++ )) ))
do
if [ $U -le $T ] && [ `expr $U + ${R[$i]}` -ge $T ]
then
x[$k]=$((x[$k]+1))
fi
let k=$k+1
done
k=0
done

Please help me..!!

Last edited by himu3118; 07-06-2010 at 03:37 AM.
 
Old 07-05-2010, 05:48 AM   #2
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
here's a suggestion:
Code:
declare -i I=0
declare -a R S LINE; R=() S=()

while read LINE; do
    R[I]=${LINE[7]}   # $(echo "$LINE" | awk 'print $8}')
    S[I]=${LINE[7]}   # $(echo "$LINE" | awk 'print $7}')
    let I++
done < "$FILE"

....
# some examples for now:

# for I in ${!ARRAY[@]}; do
# for V in "${ARRAY[@]}"; do
# VAR[I++]='value'
It's just a part of it 'cause I'm sort of out of time.. later
 
Old 07-05-2010, 06:23 AM   #3
himu3118
Member
 
Registered: Mar 2010
Posts: 35

Original Poster
Rep: Reputation: 15
now i m using:

for U in ${S[@]}
do
for (( T=1276082023 ; $T -le '1276082139' ; (( T++ )) ))
do
if [ $U -le $T ] && [ `expr $U + ${R[$i]}` -gt $T ]
then
x[$k]=$((x[$k]+1))
fi
let k=$k+1
done
let i=$i+1
k=0
done

then error is coming in second loop. i think there is some string to integer conversion problem. how to correct this problem. and how to initialize all elements of array equal to integer 0.

Last edited by himu3118; 07-06-2010 at 03:55 AM.
 
Old 07-05-2010, 07:07 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Firstly, utilising the [ code ][ \code ] delimeters (without the spaces) will make your code much more readable.

Second, perhaps if you gave us a demo of what is in the file, ie the large numbers, and the goal of the script we may be able to help more?

Lastly, a little consistency would really help with trying to understand your code: (for example)

First post -
Code:
let i=0
Yet you then pull the 'j' variable out of nowhere

From both your posts -
Code:
(( T++ ))

# and then later

let k=$k+1
Again these are the same task, to increase variable by 1, but done in different ways.

Just a thought
 
Old 07-05-2010, 09:15 AM   #5
himu3118
Member
 
Registered: Mar 2010
Posts: 35

Original Poster
Rep: Reputation: 15
the contents of files which i m reading are:
abc iau aja 97 aj aja 1276082023 5 1276082028
abc iau aja 97 aj aja 1276082023 5 1276082028


i have to read 7th and 8th arg of of each line which i m doing in first part. array S contains large numbers like 1276082023.
i want to compare value of each S with T and S+R with T and accordingly increment x array.

Last edited by himu3118; 07-06-2010 at 03:56 AM.
 
Old 07-05-2010, 09:29 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Not sure where this is going, but assuming I understand you correctly, the following might work:
Code:
awk '{for(t=1276082023; t <= 1276082139; t++)if($7 <= t && $7 + $8 >= t)x[k++]++;k=0}END{for(i=0;i<length(x);i++)print i,x[i]}' file
 
Old 07-06-2010, 03:04 AM   #7
himu3118
Member
 
Registered: Mar 2010
Posts: 35

Original Poster
Rep: Reputation: 15
thanx.. you almost solved the problem.. thanx a lot.

Last edited by himu3118; 07-06-2010 at 03:36 AM.
 
Old 07-06-2010, 03:47 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,008

Rep: Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193Reputation: 3193
Well I believe the awk above does all that was required from the bash script, at least based on the data provided.
Quote:
in that i m not able to compare values like i want to compare values of T
I have 2 suggestions here:
1. Use double square brackets as the single ones some times have peculiar affects
2. Break down your comparisons into single items to see which one is failing
Quote:
also i want to know how to declare array with some intial value as i need to declare x array with all its elements equal to 0
The issue I would see here is you do not know ahead of time how big your array is as 'k' in your script is ever increasing.
Also, if not entered then the default is to be null, are you not able to use this to do whatever test, that has not been shown, that you wish to
do to the array??
 
Old 07-06-2010, 04:02 AM   #9
himu3118
Member
 
Registered: Mar 2010
Posts: 35

Original Poster
Rep: Reputation: 15
thank you very much. now i have used double quotes and now array thing is also working...
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with arrays in bash igor.R Linux - Newbie 8 04-18-2009 11:05 AM
Bash Arrays Simon256 Programming 2 02-17-2009 01:39 PM
[bash] How do I nest for arrays? blckleprd Programming 3 06-05-2008 10:49 PM
handling arrays.....in C Veteq Programming 3 05-30-2005 03:59 PM
bash for statement with 2 arrays? Noerr Linux - General 10 05-27-2002 12:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration