Array Count and Array Copy help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Array Count and Array Copy help
# 1  
Old 07-31-2013
Array Count and Array Copy help

Hi all,

I have the code as below "
Code:
echo "File carried list after 1st loop "${fileStreamAssiagnInit[@]}""

and I have the out put for the above code as below :

Code:
Output : File carried list after 1st loop abcInd.csv sdgUS.csv sopSing.csv

Here i want to count the number of elements in the array
I tried ${#fileStreamAssiagnInit[@]} but it doesn't work.I need the output as 3 but it gives me zero.

Here one more Help required
Code:
fileStreamLoad=""ab.csv","bc.csv","cd.csv","de.csv","ef.csv","fg.csv"";
fileStreamAssign=($fileStreamLoad);
echo "File carried list after 1st loop "${fileStreamAssiagnInit[@]}"" // its being carried from some other result.
If [ $count -eq 2 ]
then
fileStreamAssign="${fileStreamAssiagnInit[@]}" // I am not able to assign the array values to another array which is already having the values.
fi
for ((i=0,j=0; i<${#fileStreamAssign[@]}; ++i));
do
if [ -f ${fileStreamAssign[$i]} ]
then
cp ${fileStreamAssign[$i]} $destPath/${fileStreamAssign[$i]}_$(date +%Y%m%d)
else
fileStreamAssiagnInit[$j]="${fileStreamAssign[$i]}"
j=$(($j+1)) 
fi 
done

If you could help that would be great thanks
# 2  
Old 07-31-2013
Hello,

For counting the number of elements in an array as per your exampple. Please try the following. Kindly let me know in case you have queries.


Code:
$ cat array_count.ksh
fileStreamLoad=""ab.csv","bc.csv","cd.csv","de.csv","ef.csv","fg.csv"";
j=0
value_fileStreamLoad=`echo $fileStreamLoad | sed 's/,/ /g; s/\"//g'`
set -A array_fileStreamLoad ${value_fileStreamLoad}
echo ${array_fileStreamLoad[@]}

for  a in ${array_fileStreamLoad[@]}
do
let "j = j +1"

done

echo $j



Output for this script is as follows.


Code:
$ ksh array_count.ksh
ab.csv bc.csv cd.csv de.csv ef.csv fg.csv
6
$



Note: You can comment the bold line in comment if you don't want to show array's elements.





Thanks,
R. Singh
# 3  
Old 07-31-2013
Code:
fileStreamLoad=""ab.csv","bc.csv","cd.csv","de.csv","ef.csv","fg.csv"";
fileStreamAssign=($fileStreamLoad);
set count=2
echo "File carried list after 1st loop "${fileStreamAssiagnInit[@]}"" //Output : File carried list after 1st loop abcInd.csv sdgUS.csv sopSing.csv
for  a in ${fileStreamAssiagnInit[@]}
do
let "p = p +1"
done
echo $p
if [ $countfun -eq 2 ]
 then
     for ((k=0; k<$p; ++k));
    do
  fileStreamAssign="${fileStreamAssiagnInit[$k]}"
        done
 
echo "File carried list fileStreamAssign "${fileStreamAssiagn[@]}"" //Output required : File carried list fileStreamAssign abcInd.csv sdgUS.csv sopSing.csv

fi

Could you help me to assign the array value to another array ?
# 4  
Old 07-31-2013
You realize you can't quote quotes, right?

You realize shell doesn't have //-style comments, right?

Your entire program is severely confused. What, exactly, is your goal here? And don't say 'assign an array to another array', tell me why you're assigning an array to another array... I suspect you're severely confused, there are probably much, much more direct ways to do what you want.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copy of array by index value fails

Hello, I have a complicated situational find and replace that I wrote in bash because I didn't know how to do everything in awk. The code works but is very slow, as expected. To create my modified file, I am looping through an array that was populated earlier and making some replacements at... (6 Replies)
Discussion started by: LMHmedchem
6 Replies

2. Shell Programming and Scripting

Count number of unique values in each column of array

What is an efficient way of counting the number of unique values in a 400 column by 1000 row array and outputting the counts per column, assuming the unique values in the array are: A, B, C, D In other words the output should look like: Value COL1 COL2 COL3 A 50 51 52... (16 Replies)
Discussion started by: Geneanalyst
16 Replies

3. UNIX for Beginners Questions & Answers

Awk: count unique element of array

Hi, tab-separated input: blabla_1 A,B,C,C blabla_2 A,E,G blabla_3 R,Q,A,B,C,R,Q output: blabla_1 3 blabla_2 3 blabla_3 5 After splitting $2 in an array, I am trying to store the number of unique elements in a variable, but have some difficulties resetting the variable to 0 before... (6 Replies)
Discussion started by: beca123456
6 Replies

4. 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

5. Programming

Array[count+1] legal?

I get weird decimal digits when I run the program below. int coe_amount; cout << "How many coefficients exist in your term? "; cin >> coe_amount; float coefficient; for (int count = 0; count < coe_amount; count ++) { ... (4 Replies)
Discussion started by: DyslexicChciken
4 Replies

6. 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

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. Shell Programming and Scripting

Beginner: Count & Sort Using Array's

Hi, I'm new to linux & bash so please forgive my ignorance, just wondering if anyone can help. I have a file (mainfile.txt) with comma deliminated values, like so: $1 $2 $3 613212, 36, 57 613212, 36, 10 613212, 36, 10 677774, 36, 57 619900, 10, 10 i need to split this file... (12 Replies)
Discussion started by: BigTOE
12 Replies

9. UNIX for Advanced & Expert Users

Array copy in ksh

Hi all, Following code in ksh is giving error: fileLimit=5 func(){ dir=&quot;$1&quot; format=&quot;$2&quot; array=&quot;$3&quot; i=0 ls -lrt $format | tail -${fileLimit} | while read f_det; do files=&quot;${f_det},&quot; ((i+=1)) done eval $(echo set -A $array '&quot;${files}&quot;') } func &quot;.&quot; &quot;*.pl&quot; &quot;a&quot; echo... (10 Replies)
Discussion started by: PRKS
10 Replies

10. 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
Login or Register to Ask a Question