Trouble with For-Loop

Hi there,

I wrote a little procedure that is supposed to subtract a wave from another for a set of data.

My dataset kinda looks like this:
a_wave0,a_wave1,a_wave2, etc
b_wave0,b_wave1,b_wave2,etc

What I want to accomplish is subtract b_wave0 from a_wave0, b_wave1 from a_wave1, etc. However, the final results don't seem to make sense. When I look at the debugger, it appears that that the loop is doing something like this:
b_wave0 from a_wave0, b_wave0 from a_wave1, b_wave0 from a_wave2, etc.

I was thinking that including an "if" statement within the loop might help, but I'm not quite sure how to do it.

Here is the procedure I wrote:

#pragma rtGlobals=3		// Use modern global access method and strict wave access.

Function AmorphousSpectra(w1,w2)		//Determines amorphous spectra by subtracting Spano fit from total spectra

	WAVE w1,w2						//w1=TotalSpectra and w2=Spano Fit
	String outputName= NameOfWave(w1)+"_A"
	
	Duplicate/O w1 $(outputName)
	Wave output = $(outputName)

	output = w1 - w2	
End

Function AmorphousAll(w1,w2,fnum)		//To use it do not include any numbers or _bl_smth in the name

	STRING w1,w2
	Variable fnum
	Variable i							//The initial values of i and j should be adjusted based on the numbers present in the original names of the wave
	Variable j
	String neww1,neww2
	
	for(i=16; i<=fnum;i=i+1)
		neww1= w1 +num2str(i)+"_bl_smth"	
	
	for(j=16; j<=fnum;j=j+1)
		neww2= w2 +num2str(j) +"_bl_smth"	
			AmorphousSpectra($(neww1),$(neww2))
	endfor
	endfor
End


Thanks for any suggestions/help!
Nevermind. Got it...


#pragma rtGlobals=3		// Use modern global access method and strict wave access.

Function AmorphousSpectra(w1,w2)		//Determines amorphous spectra by subtracting Spano fit from total spectra

	WAVE w1,w2						//w1=TotalSpectra and w2=Spano Fit
	String outputName= NameOfWave(w1)+"_A+test"
	
	Duplicate/O w1 $(outputName)
	Wave output = $(outputName)

	output = w1 - w2	
End

Function AmorphousAll(w1,w2,fnum)		//To use it do not include any numbers or _baselined_smth in the name

	STRING w1,w2
	Variable fnum
	Variable i							//The initial values of i and j should be adjusted based on the numbers present in the original names of the wave
	Variable j
	String neww1,neww2
		
		for(i=16; i<=fnum;i=i+1)
			neww1= w1 +num2str(i)+"_bl_smth"	
		for(j=16; j<=fnum;j=j+1)
			neww2= w2 +num2str(j) +"_bl_smth"	
			if(i==j)	
				AmorphousSpectra($(neww1),$(neww2))
			else
				return 0
			endif
		endfor
		endfor	
End