Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to trim all the words?

Status
Not open for further replies.

millbear

Programmer
May 16, 2002
23
SG
Anyone can correct me? (-:

When calling out the job no, the details will be displayed.

for eg : CHEVSINSMZ 100, CHEVSINSMZ 101, CHEVSINSMZ 102
I want to trim them into CHEVSINSMZ 100 & 101 & 102.

jt_hblno is the field name of CHEVSINSMZ 100..

Code as below in VFP version 6.
if k=1
if .not.(empty(jt_hblno))
arr[1,k]= "("+alltrim(str(k))
arr[2,k] = jt_hblno
k = k+1
else
k = k+1
endif
else
:
:
:
Result said:
CHEVSINSMZ 100, CHEVSINSMZ 101, CHEVSINSMZ 102

 
Code:
close all
clear
m.MyString = "CHEVSINSMZ 100, CHEVSINSMZ 101, CHEVSINSMZ 102"
m.MyName = left(m.MyString,At(" ",m.MyString))
? "["+m.MyString+"]"
m.MyString = m.MyName+ " " + strtran(strtran(m.MyString,m.MyName,""),","," & ")
? "["+m.MyName+"]"
? "["+m.MyString+"]"

Regards

Griff
Keep [Smile]ing
 
[ponder]Hmm Anyone can help out to edit my existing code instead of giving me a new code?

[COLOR=blue yellow]"CHEVSINSMZ 100, CHEVSINSMZ 101, CHEVSINSMZ 102" should not include in the coding.[/color].

jt_hblno is the field name of CHEVSINSMZ 100, CHEVSINSMZ 101, so on..



Below is to find any serial {e.g. HBL no}
Code:
if k=1
if .not.(empty(jt_hblno))
arr[1,k]= "("+alltrim(str(k))
arr[2,k] = jt_hblno
k = k+1
else
k = k+1
endif
else

for l = 1 to k-1
if k = 2
if .not.(empty(jt_hblno)) .and. .not.(empty(arr[1,l]))if .not.(jt_hblno = arr[2,k-1])
arr[1,k]= "("+alltrim(str(k))
arr[2,k] = jt_hblno  
k = k+1
exit 

else

tmp = arr[1,l]
arr[1,k] = alltrim(tmp)+","+alltrim(str(k))
k = k+1
exit
endif

else

if .not.(empty(jt_hblno))
arr[1,k] = "("+alltrim(str(k))
arr[2,k] = jt_hblno
endif
k = k+1
endif

else

if .not.(empty(jt_hblno)) .and. .not.(empty(arr[1,l]))
if .not.(jt_hblno = arr[2,l])   
if l = k-1 
arr[1,k] = "("+alltrim(str(k))
arr[2,k] = jt_hblno
k = k+1
exit

endif

else
tmp = arr[1,l] 
arr[1,l] = alltrim(tmp)+","+alltrim(str(k))
k = k+1
exit

endif

else
if .not.(empty(jt_hblno))
arr[1,l] = "("+alltrim(str(k))
endif
k = k+1

exit

endif

endif

-- Below is the display of the HBL no on the form --
Code:
for l = 1 to k-1 
if l = 1
if .not.(empty(arr[1,1]))
hblno1 = arr[1,l]+") "+alltrim(arr[2,l])
endif

else

if .not.(empty(arr[1,l]))
hblno1 = hblno1+" & "+arr[1,l]+") "+alltrim(arr[2,l]		
endif

endif 
endfor 

[COLOR=green][B][I]The result is CHEVSMZSIN 101, CHEVSMZSIN 102, CHEVSMZSIN 103[/I][/B][/color]

[!]How to trim all the words into "CHEVSMZSIN 101 & 102 & 103" [/!]



[!][/!]
 
I'm not quite following your code.

There doesn't seem to be an ENDFOR to match your

Code:
for l = 1 to k-1

And what value is k normally initiallised at?



Regards

Griff
Keep [Smile]ing
 
After ENDFOR line, the next following code as below :

Code:
.xdf_bl.value= hblno1

.xdf_bl.value is the name of textbox in the form.

It is quite hard to describe the actual output..
 
By the way, that original sample of code was meant to help you, by demonstrating how to process you finished output into the form you wanted.

Presumably you have some code somewhere that places the line
Code:
CHEVSMZSIN 101, CHEVSMZSIN 102, CHEVSMZSIN 103

into a text box, or caption on your form. That would look something like:
Code:
.xdf_bl.value= hblno1

If you turn the code I gave you into a visible function as below:
Code:
FUNCTION MyProcessString
	PARAMETER m.MYSTRING
	PRIVATE m.MYSTRING,m.MYNAME
	M.MYNAME = LEFT(m.MYSTRING,AT(" ",m.MYSTRING))
	M.MYSTRING = m.MYNAME+ " " + STRTRAN(STRTRAN(m.MYSTRING,m.MYNAME,""),","," & ")
	RETURN(m.MYSTRING)

You could call it just after you change the value in the textbox - thereby negating the need to modify any of the rest of your code:

Code:
.xdf_bl.value= hblno1
.xdf_bl.value= MyProcessString(.xdf_bl.value)



Regards

Griff
Keep [Smile]ing
 
It seem not working on my form.
BTW, what version you are using foxpro software?[upsidedown]
 
>>Anyone can help out to edit my existing code instead of giving me a new code?

frankly speaking, you code looks too big for stated task and hard to follow.

Below is code I just tested at my place; youy can adapt it to your needes (insert more checks etc).
It scans table with field
Jt_hblno
containing
CHEVSMZSIN 101
CHEVSMZSIN 102
CHEVSMZSIN 103

Code:
hblno1=""
i=1
scan
	if i=1
		hblno1=alltrim(Jt_hblno)
	else
		hblno1=hblno1+" &"+substr(alltrim(Jt_hblno),at(" ",alltrim(Jt_hblno)))
	endif
	i=i+1
endscan
?hblno1

Output is:
CHEVSMZSIN 101 & 102 & 103
 
I think that is because your code can't 'see' the new function.

You could replace your line:

Code:
.xdf_bl.value= hblno1

with

Code:
.xdf_bl.value= LEFT(hblno1,AT(" ",hblno1 )) + " " + STRTRAN(STRTRAN(hblno1 ,LEFT(hblno1 ,AT(" ",hblno1 )),""),","," & ")

Regards

Griff
Keep [Smile]ing
 
sorry GriffMG, my window was open since your post of 22 May 06 2:36. I don't mean to interfere that much ;)
 
I am thoroughly confused at what you are trying to accomplish.

Based on what you say, it seems as if the field jt_hblno contains something like "CHEVSINSMZ 100, CHEVSINSMZ 101, CHEVSINSMZ 102" and the result you want is "CHEVSINSMZ 100 & 101 & 102".

Then I look at your code and don't understand how it is supposed to accomplish this. It looks like an awful lot of code to accomplish simple string extractions that are stitched back together into a different string.

My questions are:

1) Are you extracting all values consecutively, or are you skipping some? It looks to me that you are using all consecutively.

2) Are you always stitching them back together in the same order that they are in the original string? It looks so to me.

3) What is the array for? Just to temporarily save the values or are you using the contents of the array somewhere else in your code? If this is the only place you are using it, what is the purpose of using it? Wouldn't it be simpler (and easier to read and debug) just to use temporary variables instead?

It seems to that you need to set up a function of some sort, maybe something like Griff's code, and call it as needed. Once such a function is setup and debugged, you could call it at any time and output the results as needed.

Even though I am thoroughly confused, I do see a problem.

[tt]if .not.(empty(arr[1,l]))
hblno1 = hblno1+" & "+arr[1,l]+") "+alltrim(arr[2,l][COLOR=red yellow])[/color]        
endif[/tt]

Don't know if that is causing the problems you are having, but it will cause the program to crash.


mmerlinn

"Political correctness is the BADGE of a COWARD!"

 
It's cool tsh, I had thought the exact same thing... that's pretty much 'write only code' I think...

I can't see HOW Bear gets the sting he says comes out of it into the form he's getting B-) but I do know that's not ALL the code - so perhaps another bit does the work?

Martin

Regards

Griff
Keep [Smile]ing
 
I have tried both tsh73 and griffms's suggested code.
The result is " (1) & & "

Sorry to confuse you earlier.
I have added simple description : Invoice.

As you see the [highlight]yellow box[/highlight] - Job No:, I enter any job no to call out and all the details will be displayed. I'm struck with the B/L no field. I'm trying to trim all the serial no with repeated character into CHEVSINSMZ 100 & 101 & 102..

The BL no can be any serial nos. E.g. SCSPSINSMZ 33, SCSPSINSMZ 34, so on...
--------------------------------------------------------------------------------------------------------------
if .not.(empty(arr[1,l]))
hblno1 = hblno1+" & "+arr[1,l]+") "+alltrim(arr[2,l])
endif
If there are 4 HBL nos (CHEVSINSMZ 100, CHEVSINSMZ 100, CHEVSINSMZ 101, CHEVSINSMZ 102) displayed after calling out the Job No,
The Result is (1,2) CHEVSINSMZ 100& (3) CHEVSINSMZ 101 & (4) CHEVSINSMZ 102.

I don't want to have this original result. All I want to see is (4) CHEVSINSMZ 100 & 101 & 102.









 
That's a slightly different question - the input AND the output strings are different!

I was wondering why your original output had no figures in brackets, when most of the code sample seemed to include it.

Can you post the entire code - rather than just a segment - so it can be read in full?

Martin

Regards

Griff
Keep [Smile]ing
 
Here is entire code as u requested. (-:

Code:
local i, lchar, lnum, jn, lcnt, ltmp, lncargo, pono, ordrno, hblno1, hblno2, k, tmp, lhbl
dimension lcargo(12)
dimension lpono(12)
dimension lordrno(12)
dimension arr[2,25]
ex = 0
vess = ""

with thisform.pf.pg1
	.xdf_bl.value 	  	= ""
	.xdf_bl2.value 	  	= ""  && Add
	.xdf_indent.value	= ""
	.xdf_indent2.value 	= ""
	.xdf_pol.value  	= ""
	.xdf_pod.value  	= ""
	.xdf_dterm.value  	= ""
	.xdf_vessel.value  	= ""
	.xdf_cargo.value  	= ""
	.xdf_cargo2.value  	= ""
	.xdf_cargo3.value  	= ""
	.xdf_ntank.value  	= ""
	.xdf_tank1.value  	= ""
	.xdf_tank2.value  	= ""
	.xdf_tank3.value  	= ""
	.xdf_tank4.value  	= ""  && Add
	.xdf_others.value 	= ""
	.xdf_etd.value  	= {}
	.xdf_eta.value  	= {}

	select dbfidgl
	jn = .xdf_jn.value
	if .not. empty(jn) then
		for i=1 to 99
			goto i
			if empty(di_pre) then
				loop
			endif
			lchar = alltrim(di_pre)
			lnum  = len(lchar)
			if lchar = left(jn,lnum) then
				with thisform.pf.pg2
					.dtdgl.value = di_dtdgl
					.crevgl.value = di_crevgl
					.cgstgl.value = di_cgstgl
					.dtdglname.value = space(40)
					.crevglname.value = space(40)
					.cgstglname.value = space(40)
				endwith
				exit
			endif
		next i
	endif

	select tnfjob
	set order to tnfjob
	locate for (jt_jobno = jn) and (not(empty(jt_fvess)))
	if found()
		ex = 1
		.xdf_pol.value = jt_fpol
		vess = alltrim(jt_fvess)
		.xdf_etd.value   = jt_fdpod
	else
		continue
	endif

	select abkng
	set order to bkng
	seek jn
	if found() and not(empty(jn))
		if ex = 0
			.xdf_pol.value   = bkg_pol
			vess = bkg_vessel
			.xdf_etd.value   = bkg_etdpol
		else
			vess = vess+" / "+bkg_vessel
		endif
		.xdf_pod.value   = bkg_pod
		.xdf_dterm.value = bkg_term
		.xdf_vessel.value= vess
		.xdf_eta.value   = bkg_etapod								
		.xdf_indent.value= bkg_pono
		lcnt = 0
		
		if (alltrim(.xdf_deb.value) = alltrim(bkg_cgnee))			
			.xdf_cont.value = bkg_cntcgn
		else
			if (alltrim(.xdf_deb.value) = alltrim(bkg_shiper))
				.xdf_cont.value = bkg_cntshp
			endif
		endif
		
		select port					 								
		set order to port
		seek .xdf_pol.value
		.xdf_pol.value = pt_name
		seek .xdf_pod.value
		.xdf_pod.value = pt_name
		
		store space(70) to lcargo
		store space(30) to lpono
		store space(30) to lordrno
		lncargo = 0
		ltmp = ""
		pono = ""
		ordrno = ""
		tmp = ""
		hblno1 = ""  && Add
		hblno2 = ""  && Add
		m = 1
		n = 1
		s = 1
		eq = 0
		k = 1

		select tnfjob
		set order to tnfjob
		locate for (jt_jobno = jn)
		do while found()
			lcnt = lcnt + 1
	 		do case
				case lcnt <= 3
					.xdf_ntank.value = 	.xdf_ntank.value + jt_tankno + ' '
				case lcnt > 3 .and. lcnt <= 7
					.xdf_tank1.value = 	.xdf_tank1.value + jt_tankno + ' '
				case lcnt > 7 .and. lcnt <= 11
					.xdf_tank2.value = 	.xdf_tank2.value + jt_tankno + ' '
				case lcnt > 11 .and. lcnt <= 15
					.xdf_tank3.value = 	.xdf_tank3.value + jt_tankno + ' '
				case lcnt > 15 .and. lcnt <= 19
					.xdf_tank4.value = 	.xdf_tank4.value + jt_tankno + ' '  && Add
			endcase
			
*		    do case
				** Break into 2 lines in HBL no in the box
*				case lcnt <= 2
*					.xdf_bl.value = 	.xdf_bl.value + jt_hblno + ' '
*				case lcnt > 2 .and. lcnt <= 4
*					.xdf_bl2.value = 	.xdf_bl2.value + jt_hblno + ' '
*	    	endcase
			
			if .not. empty(jt_pronme)
				j = 0
				for i = 1 to lncargo
					if jt_pronme = lcargo(i)
						j = i
						exit
					endif
				next i
				if j=0
					lncargo = lncargo + 1
					if lncargo > 12
						lncargo = 12
					endif
					lcargo(lncargo) = jt_pronme
				endif
			endif

				
			*/* Purchase Order (PO no: )
			if .not. empty(jt_pono)
				if m = 1
					lpono(m) = alltrim(jt_pono)
					m = m + 1
				else
					eq = 0
					for i = 1 to m-1
						if upper(lpono(i)) = upper(alltrim(jt_pono))
							eq = 1
							exit
						endif
					endfor
					if eq = 0
						lpono(m) = alltrim(jt_pono)
						m = m+1
					endif
				endif
			endif
			
			*/* Order No 
			if .not. empty(jt_ordrno)
				if s = 1
					lordrno(s) = alltrim(jt_ordrno)
					s = s + 1
				else
					eq = 0
					for i = 1 to s-1
						if upper(lordrno(i)) = upper(alltrim(jt_ordrno))
							eq = 1
							exit
						endif
					endfor
					if eq = 0
						lordrno(s) = alltrim(jt_ordrno)
						s = s+1
					endif
				endif
			endif
			
		*/* HBL no			
			if k=1
				if .not.(empty(jt_hblno))
					arr[1,k]= "("+alltrim(str(k))
					arr[2,k] = jt_hblno
					k = k+1
				else
					k = k+1
				endif
			else
				for l = 1 to k-1
					if k = 2
						if .not.(empty(jt_hblno)) .and. .not.(empty(arr[1,l]))
							if .not.(jt_hblno = arr[2,k-1])
								arr[1,k]= "("+alltrim(str(k))
								arr[2,k] = jt_hblno  
								k = k+1
								exit 
							else
								tmp = arr[1,l]
								arr[1,k] = alltrim(tmp)+","+alltrim(str(k))
								k = k+1
								exit
							endif
						else
							if .not.(empty(jt_hblno))
								arr[1,k] = "("+alltrim(str(k))
								arr[2,k] = jt_hblno
								endif
							k = k+1
						endif
					else
						if .not.(empty(jt_hblno)) .and. .not.(empty(arr[1,l]))
							if .not.(jt_hblno = arr[2,l])   
 
								if l = k-1 
									arr[1,k] = "("+alltrim(str(k))
								    arr[2,k] = jt_hblno
									k = k+1
									exit
								endif
							else
								tmp = arr[1,l] 
								arr[1,l] = alltrim(tmp)+","+alltrim(str(k))
								k = k+1
								exit
							endif
						else
							if .not.(empty(jt_hblno))
								arr[1,l] = "("+alltrim(str(k))
							endif
							k = k+1
							exit
						endif
					endif
				endfor	
			endif
			select tnfjob
			continue
		enddo
		.xdf_ntank.value = 	'('+left(str(lcnt,2)+')   '+.xdf_ntank.value,60)
		.xdf_cargo.value  = lcargo(1)
		.xdf_cargo2.value = lcargo(2)
		.xdf_cargo3.value = lcargo(3)

		for i = 1 to m-1									
			if i = 1 
				pono = lpono(i)
			else
				pono = pono+" & "+lpono(i)
			endif
		next i
		
		for i = 1 to s-1
			if i = 1 
				ordrno = lordrno(i)
			else
				ordrno = ordrno+" & "+lordrno(i)
			endif
		next i
		
		for l = 1 to k-1 
			if l = 1
					if .not.(empty(arr[1,l]))
					hblno1 = arr[1,l]+") "+alltrim(Jt_hblno)
					endif

		else
				if .not.(empty(arr[1,l]))
					hblno1=hblno1+" &"+substr(alltrim(Jt_hblno),at(" ",alltrim(Jt_hblno)))
   		 endif
   				 i=i+1

				endif
		endfor 
		
*		for l = 1 to k-1 
*			if l = 1
*				if .not.(empty(arr[1,l]))
*					hblno1 = arr[1,l]+") "+alltrim(arr[2,l])
*				endif

*		else
*				if .not.(empty(arr[1,l]))
*					hblno1 = hblno1+" & "+arr[1,l]+") "+alltrim(arr[2,l])
*				endif
*			endif 
*		endfor 


		
		.xdf_indent.value= pono
		.xdf_indent2.value= ordrno
		.xdf_bl.value= hblno1
	&&	.xdf_bl.value= LEFT(hblno1,AT(" ",hblno1 )) + " " + STRTRAN(STRTRAN(hblno1 ,LEFT(hblno1 ,AT(" ",hblno1 )),""),","," & ")

*		.xdf_bl2.value= hblno2	&& add
	endif


*/* For the job with "K" . Copy Original coding 22/05/06

*	if substr(.xdf_jn.value,1,1)="K"
*		x = 0
*		y = 0
*		i = 1
*		ad = 1
*		tank = ""
*		bl = ""
*		lncargo = 0
*		store space(20) to arr
*		store space(70) to lcargo
*		select tffile
*		set order to tffile
*		seek .xdf_jn.value
*		do while found() .and. (tf_jobno = .xdf_jn.value)
*			x = x+1
*			if x = 1
*				tank = "CONTAINER NO / FLEXITANK NO:-"+chr(13)+str(x,1)+")"+tf_cntno+" / "+tf_flxno+chr(13)
*			else
*				tank = tank+str(x,1)+")"+tf_cntno+" / "+tf_flxno+chr(13)
*			endif
			
*			if i = 1
*				arr[1,i] = alltrim(tf_hblno)
*				bl = alltrim(tf_hblno)
*				y = 1
*			endif
*			ad = 1
*			for z = 1 to y
*				if (alltrim(arr[1,z]) = alltrim(tf_hblno))
*					ad = 0
*				endif
*			next z
*			if i > 1
*				if ad = 1
*					arr[1,y+1] = alltrim(tf_hblno) 
*					bl = bl+" & "+alltrim(tf_hblno)
*					y = y+1 
*				endif
*			endif
			
*			if .not. empty(tf_cargo)
*				j = 0
*				for i = 1 to lncargo
*					if tf_cargo = lcargo(i)
*						j = i
*						exit
*					endif
*				next i
*				if j=0
*					lncargo = lncargo + 1
*					if lncargo > 12
*						lncargo = 12
*					endif
*					select cargo
*					set order to cargo
*					seek tffile.tf_cargo
*					if found()
*						lcargo(lncargo) = crg_prod
*					endif
*					select tffile
*				endif
*			endif
*			i = i+1
*			skip
*		enddo
*		.xdf_bl.value = bl
*		.xdf_indent2.value= abkng.bkg_ordrno
*		if not(abkng.bkg_mft20 = 0) or not(empty(abkng.bkg_mft20))
*			.xdf_ntank.value = alltrim(str(abkng.bkg_mft20))+" x 20'"
*		endif
*		if not(abkng.bkg_mft40 = 0) or not(empty(abkng.bkg_mft40))
*			.xdf_tank1.value = alltrim(str(abkng.bkg_mft40))+" x 40'"
*		endif
*		.xdf_cargo.value  = lcargo(1)
*		.xdf_cargo2.value = lcargo(2)
*		.xdf_cargo3.value = lcargo(3)
*		.xdf_others.value = tank
*	endif
endwith
 
Please modify the code below appropriately to suit your program.

Code:
LOCAL InputString, CommaPos, i, SpacePos, PrefixString, BlNo
STORE 0 TO CommaPos, i, Counter, SpacePos
LOCAL ARRAY xAr[1]
xAr[1] = ""
PrefixString = ""
BlNo = ""
InputString= "CHEVSINSMZ 100, CHEVSINSMZ 100, CHEVSINSMZ 101, CHEVSINSMZ 102"
CommaPos = AT(",",InputString)
SpacePos = AT(" ", InputString)
PrefixString = SUBSTR(InputString, 1, SpacePos - 1)
DO WHILE CommaPos > 0
	Counter = Counter + 1
	BlNo = STRTRAN(SUBSTR(InputString,1, CommaPos-1), PrefixString, "")
	IF ASCAN(xAr, BlNo) = 0
		i = i + 1
		DIMENSION xAr[i]
		xAr[i] = BlNo
	ENDIF 
	InputString = ALLTRIM(SUBSTR(InputString,CommaPos+1))
	CommaPos = AT(",",InputString)
ENDDO
BlNo = STRTRAN(InputString, PrefixString, "")
IF ASCAN(xAr, BlNo) = 0
	DIMENSION xAr[i+1]
	xAr[i+1] = ALLTRIM(BlNo)
	Counter = Counter + 1
ENDIF
OutPutString = "("+ALLTRIM(str(Counter))+") " + PrefixString + " "
FOR j = 1 TO ALEN(xAr)
	IF j = 1
		OutPutSTring = OutPutSTring + xAr[j]
	ELSE
		OutPutSTring = OutPutSTring + " & " + xAr[j]
	ENDIF
NEXT j
?outputstring

Hope this helps.

Rajesh
 
InputString= "CHEVSINSMZ 100, CHEVSINSMZ 100, CHEVSINSMZ 101, CHEVSINSMZ 102"

Is it possible to work if i rewrite the existing code to InputString= "" ?

The purpose of my form is only searching for any job no. All the details including BL no will be displayed itself.. There is no exactly same BL no..
 
I am sorry, but I don't understand your question.

My post contains the algorithm to convert a string ("InputString" in the code) to the format that you wanted.
Please substitute this with Form.AppropriateControl.value.
Rajesh
 
millieniumbear

Did you try cut-and-pasting the revised code that I posted?

Was it close to what you need?



Regards

Griff
Keep [Smile]ing
 
Click Invoice.

I key in the company code and then enter to the Job no: field. All the details including CHEVSINSMZ 101, CHEVSINSMZ 102,or any words are displayed..

So I'm not sure if i remove the input string "CHEVSINSMZ", the form will work or not.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top