Add value to end of array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Add value to end of array
# 1  
Old 01-08-2013
Add value to end of array

I'm trying to expand my array by adding another value onto the end of it, thus adding a new index and upping the length of the array by one. I am iterating through two arrays, and trying to get one array into the index of the other. I'm use 4.1.5 release of bash and most of the methods I have tried are outdated.

This is my code...
Code:
for ((i=1; i<=${COUNT1}; i++))
do
	for ((j=1; j<=${COUNT2}; j++))
	do
		if [[ ${MAIN_ARRAY[i]} != ${SECOND_ARRAY[${j}]} ]]
		then
                      #Put the value of SECOND_ARRAY in MAIN_ARRAY
		fi
	done
done

I originally tried the += method and it did not work.
Code:
${ARRAY}+=${NEW_VALUE[${i}]}

Then I tried adding one to the length of the array.
Code:
q=`echo ${#ARRAY[@]}`
let q++ 
ARRAY[${q}]=${SECOND_ARRAY[${j}]}

Again, I am trying to iterate through two arrays, and if the the value of the second array is not in the first, put it in there at the end.
# 2  
Old 01-08-2013
That arrays make loops such a pain is one reason I think they're overused.

Code:
A1="a b c d e f g"
A2=" q i u d e f g"

for X in $A1
do
        FOUND=0

        for Y in $A2
        do
                [ "$X" = "$Y" ] && FOUND=1
                [ "$FOUND" -gt 0 ] && break
        done

        [ "$FOUND" -eq 0 ] && A1="$A1 $X"
done

But if you must use arrays, that kind of loop works with them too:

Code:
for X in "${A1[@]}"
do
        FOUND=0

        for Y in "${A2[@]}"
        do
                [ "$X" = "$Y" ] && FOUND=1
                [ "$FOUND" -gt 0 ] && break
        done

        [ "$FOUND" -eq 0 ] && A1=( "${A1[@]}" $X )
done

# 3  
Old 01-08-2013
One way with recent bash versions:

Code:
$ a1=( a b c d ) a2=( c d e f )
$ declare -p a1 a2
declare -a a1='([0]="a" [1]="b" [2]="c" [3]="d")'
declare -a a2='([0]="c" [1]="d" [2]="e" [3]="f")'
$ a1+=($( printf '%s\n' "${a2[@]}" | grep -Fxvf <( printf '%s\n' "${a1[@]}" )))
$ declare -p a1
declare -a a1='([0]="a" [1]="b" [2]="c" [3]="d" [4]="e" [5]="f")'

Values with special characters may need special attention.

P.S. declare -p is used only to display the content of the arrays.

Last edited by radoulov; 01-08-2013 at 12:49 PM..
This User Gave Thanks to radoulov For This Post:
# 4  
Old 01-08-2013
I'm new at bash, and do not understand some of your code.

What is the
Code:
[..]
[..]

in the for loop?

I'm assuming its like an if? Saying if X=Y then make found=1?
# 5  
Old 01-08-2013
You already know what [ ] do if you've used if-statements before. It's actually an independent thing, if just makes use of it. It returns success when the statement within is true, or error when false.

&& is a short-form if-then. If the thing on the left returns success, run the thing on the right. || is the opposite -- if the thing on the left returns error, run the thing on the right.

Code:
true && echo "This will print"
false && echo "But this won't"

true || echo "This will not print"
false || echo "But this will"

This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add text to the end of line

Seems simple but ive been searching for a good hour of so I have a text file and would like to add a string to the end of line 5 ( as an example) to ake tings hard the line number we have to add the text to is stored in a variable cunningly name $Line_to_append any ideas on how this could... (2 Replies)
Discussion started by: dunryc
2 Replies

2. Shell Programming and Scripting

Add a new field at the end of each line

i want to add a white-space at the end of each line for my inp.file, but when i do it, the result is a new line with a white-space between each line! my input: 2012 0811 1223 15.2 L 38.393 46.806 9.0 Teh 78 0.5 6.5LTeh 1 GAP=74 ... (5 Replies)
Discussion started by: saeed.soltani
5 Replies

3. Shell Programming and Scripting

Add line at the end

How to add a comma at the end of each line in this file?30 1412 30 3352 30 5254 30 5543 30 7478 3 28 3 30 3 39 3 54 3 108 3 152 3 178 3 182 3 214 3 271 3 286 3 300 3 348 3 349 3 371 (3 Replies)
Discussion started by: gunjan
3 Replies

4. Shell Programming and Scripting

how to add ; at the end of last line

hi, i have file which is having large sql query eg : i am executing this sql file but now i want to add ; after query on same line i.e. i should look like any idea how to achieve it ? (6 Replies)
Discussion started by: crackthehit007
6 Replies

5. Shell Programming and Scripting

To add a number at the end of the line

Hi Folks, Using the Vi, how can I add a numbers at the end of the line. For eg: I have the numbers in the file as: 58.125.33 22.58.68 25.144.225 114.25.38 I need to add .0/8 at the end of all the line. So, it should be like 58.125.33.0/8 22.58.68.0/8 25.144.225.0/8 114.25.38.0/8 (6 Replies)
Discussion started by: gsiva
6 Replies

6. Shell Programming and Scripting

Add end of char \n on end of file

Hi, I want to add \n as a EOF at the end of file if it does't exist in a single command. How to do this? when I use command echo "1\n" > a.txt and od -c a.txt 0000000 1 \n \n 0000003 How does it differentiate \n and eof in this case? Regards, Venkat (1 Reply)
Discussion started by: svenkatareddy
1 Replies

7. Shell Programming and Scripting

Accessing single elements of a awk array in END

How do I access one of the indices in array tst with the code below? tst=sprintf("%5.2f",Car / 12) When I scan thru the array with for ( i in tst ) { print i,tst } I get the output of: vec-7 144 But when I try this in the END print tst It looks like it's not set. What am... (6 Replies)
Discussion started by: timj123
6 Replies

8. Shell Programming and Scripting

array access in END block failure

Hi guys i am new to shell scripting. I wrote this script that simply searches a column value of file1 from file2. please look at the code below: awk ' FILENAME==ARGV { file_1_data=$0; next } FILENAME==ARGV { file_2_data=substr($3,1,12); next } END { ... (5 Replies)
Discussion started by: fahadaizaz
5 Replies

9. Shell Programming and Scripting

Add a new end of line

Hi, Does anyone know if its possible to add something like an end of line like c or java in unix? dirs=/home/nosnam var='' for dir in $dirs do listDirs=`ls -d1 $dir/*` for eachList in $listDirs do listRepos=`du -ks $eachList | awk '{ x+=$1 }; END { print x... (4 Replies)
Discussion started by: nosnam
4 Replies

10. Shell Programming and Scripting

Add a comma at end of every line

hello A small shell scripting help.. I have a file say with 5 lines of text (text file). At the end of everyline I need to add a comma at the end of the file. Thanks, ST2000 (4 Replies)
Discussion started by: ST2000
4 Replies
Login or Register to Ask a Question