Array not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Array not working
# 1  
Old 05-26-2010
Array not working

Hi
My below code to display the contents in array is not working. Pls correct my code.

Code:
cat $file | while read file1
do
   filenames[$i]=$file1
   let i=$i+1
   echo $i
echo ${filenames[$i]}
done

let j=1

while [ $j -le $i ]
do
    echo "${filenames[j]}"
    let j=$j+1
done

# 2  
Old 05-26-2010
First view .. You Missed ..

Quote:
Originally Posted by Sekar1
Hi
My below code to display the contents in array is not working. Pls correct my code.

Code:
cat $file | while read file1
do
   filenames[$i]=$file1
   let i=$i+1
   echo $i
echo ${filenames[$i]}
done

let j=1

while [ $j -le $i ]
do
    echo "${filenames[$j]}"
    let j=$j+1
done

# 3  
Old 05-26-2010
Hi ..

Even though it is not working. I am getting only below output


Code:
 . ./aa.sh
2
3
4
5

# 4  
Old 05-26-2010
Code:
#!/usr/bin/ksh

i=1
while read file1
do
   filenames[$i]=$file1
   (( i += 1))
done<${file}

j=1

while (( j < i ))
do
   echo ${filenames[$j]}
   (( j += 1))
done

# 5  
Old 05-26-2010
You are showing ${filenames[$i]} after incrementing i....
# 6  
Old 05-26-2010
Thanks.. That's working :-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Writing a Targa file not working from an array

Hello, I wrote code that generates an image that writes its contents to a Targa file. Due to modifications that I wish to do, I decided to copy the value of each pixel as they are calculated to a dynamically allocated array before write it to a file. The problem is now that I all I see is a big... (2 Replies)
Discussion started by: colt
2 Replies

2. Shell Programming and Scripting

Bash arrays: rebin/interpolate smaller array to large array

hello, i need a bit of help on how to do this effectively in bash without a lot of extra looping or massive switch/case i have a long array of M elements and a short array of N elements, so M > N always. M is not a multiple of N. for case 1, I want to stretch N to fit M arrayHuge H = (... (2 Replies)
Discussion started by: f77hack
2 Replies

3. Shell Programming and Scripting

Bash question: working with an array of previously set variable strings

while i've used arrays to work with variables, i've never used them to loop through a set of strings and wanted to ask the community for some feedback or assistance. let me be specific. here's my code: # URL port Variables port2195=`nc -z $url2195 2195` port2196=`nc -z $url2196 2196`... (5 Replies)
Discussion started by: hungryd
5 Replies

4. Shell Programming and Scripting

Bash 3.2 - Array / Regex - IF 3rd member in array ends in 5 digits then do somthing...

Trying to do some control flow parsing based on the index postion of an array member. Here is the pseudo code I am trying to write in (preferably in pure bash) where possible. I am thinking regex with do the trick, but need a little help. pesudo code if == ENDSINFIVEINTS ]]; then do... (4 Replies)
Discussion started by: briandanielz
4 Replies

5. Shell Programming and Scripting

Print arguments using array elements not working?

Hey guys, I'm new to shell scripting and I'm trying to write a script that takes user input and copies the specified columns from a data file to a new one. In order to account for the possibility of a variable number of columns to copy I wrote a loop that encodes the user's choices in an array... (16 Replies)
Discussion started by: shaner
16 Replies

6. Shell Programming and Scripting

working with null elements in an array

i have an array (with each element length "n") which is dynamic and has alphanumeric characters. i want to check if any of the elements of the array are null and replace it with a string of "n" zeros for that element. can you suggest me a code for the same. (1 Reply)
Discussion started by: mumbaiguy07
1 Replies

7. Shell Programming and Scripting

PERL : Read an array and write to another array with intial string pattern checks

I have an array and two variables as below, I need to check if $datevar is present in $filename. If so, i need to replace $filename with the values in the array. I need the output inside an ARRAY How can this be done. Any help will be appreciated. Thanks in advance. (2 Replies)
Discussion started by: irudayaraj
2 Replies

8. Programming

C programming working with multidimensional array

Hi, I have the following variable declaration which looks like a 3d array or N matrixs KxK of floats float (*table); I have to pass to a function only the first table. How can I do it?? Thanks (6 Replies)
Discussion started by: littleboyblu
6 Replies

9. Programming

Creating an array to hold posix thread ids: Only dynamic array works

I am facing a strange error while creating posix threads: Given below are two snippets of code, the first one works whereas the second one gives a garbage value in the output. Snippet 1 This works: -------------- int *threadids; threadids = (int *) malloc (num_threads * sizeof(int)); ... (4 Replies)
Discussion started by: kmehta
4 Replies

10. Shell Programming and Scripting

looping a array inside inside ssh is not working, pls help

set -A arr a1 a2 a3 a4 # START ssh -xq $Server1 -l $Username /usr/bin/ksh <<-EOS integer j=0 for loop in ${arr} do printf "array - ${arr}\n" (( j = j + 1 )) j=`expr j+1` done EOS # END ========= this is not giving me correct output. I... (5 Replies)
Discussion started by: reldb
5 Replies
Login or Register to Ask a Question