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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using an array 1

Status
Not open for further replies.

astech

Programmer
Jul 25, 2000
59
ID
i have parameter named pUser.

pUser = 'UserA,UserB,UserC,UserD'

can i break it to array to be :

array(1)='UserA'
array(2)='UserB'
array(3)='UserC'
array(4)='UserD'


can you help me, please ...
 
I think this will do it.


pUser = 'UserA,UserB,UserC,UserD'
lnLoopCount = OCCURS(",",pUser )
DECLARE laMyarray(lnLoopCount+1)

FOR lni = 1 TO lnLoopCount + 1
lnPos = ATC(",",pUser)
IF lnPos > 0
lcValue = LEFT(pUser,(m.lnPos -1))
pUser = SUBSTR(pUser,ATC(",",pUser)+1)
ELSE
lcValue = pUser
ENDIF
laMyarray(lni) = lcValue
ENDFOR

FOR lni = 1 TO ALEN(laMyarray,1)
MESSAGEBOX(laMyarray(lni))
ENDFOR

Jim Osieczonek
Delta Business Group, LLC
 
Here are two (or four) methodologies. As you can see, a string is already kind of a one dimesion array, so I'm not sure why you's want to go through extra steps to place it into an array...

Brian

Code:
pUser = 'UserA,UserB,UserC,UserD'

IF OCCURS(",",pUser)>0
  FOR lnWordCnt = 1 to OCCURS(",",pUser)
    IF lnWordCnt=1
      ?SUBSTR(pUser,1,ATC(",",pUser,1)-1)
     ELSE  
  	  ?SUBSTR(pUser,ATC(",",pUser,lnWordCnt-1)+1,ATC(",",pUser,lnWordCnt)-ATC(",",pUser,lnWordCnt-1)-1)
  	ENDIF
  ENDFOR
ENDIF

pUser = 'UserE,UserF,UserG,UserH'

 IF OCCURS(",",pUser)>0
  FOR lnWordCnt = 1 to GETWORDCOUNT(pUser,",")
   ?GETWORDNUM(pUser,lnWordCnt,",")
  ENDFOR
 ENDIF
 
pUser = 'UserI,UserJ,UserK,UserL'
 
DIMENSION laUsers[OCCURS(",",pUser)]
IF OCCURS(",",pUser)>0
  FOR lnWordCnt = 1 to OCCURS(",",pUser)
    IF lnWordCnt=1
      laUsers[lnWordCnt]=SUBSTR(pUser,1,ATC(",",pUser,1)-1)
     ELSE  
  	  laUsers[lnWordCnt]=SUBSTR(pUser,ATC(",",pUser,lnWordCnt-1)+1,ATC(",",pUser,lnWordCnt)-ATC(",",pUser,lnWordCnt-1)-1)
  	ENDIF
  ENDFOR
ENDIF

FOR lnArrayCnt = 1 TO ALEN(laUsers)
 ?laUsers(lnArrayCnt)
ENDFOR

pUser = 'UserM,UserN,UserO,UserP'

DIMENSION laUsers2[GETWORDCOUNT(pUser,",")]
IF OCCURS(",",pUser)>0
  FOR lnWordCnt = 1 to GETWORDCOUNT(pUser,",")
   laUsers2[lnWordCnt]=GETWORDNUM(pUser,lnWordCnt,",")
  ENDFOR
ENDIF

FOR lnArrayCnt = 1 TO ALEN(laUsers2)
 ?laUsers2(lnArrayCnt)
ENDFOR
 
Here's, perhaps, a neater way:
Code:
pUser = 'UserA,UserB,UserC,UserD'
LOCAL myArray[1], nCount
nCount = ALINES( myArray, STRCOUNT(pUser,',',chr(13)) )
for lnI = 1 to nCount
  ?"MyArray",lnI,myArray[lnI]
endfor
 
Note: You need VFP 7/8 to make wgcs ALINES() code work.

Rick
 
It works fine in VFP6 (which is what I develop in most of the time).... VFP7/8 add the ability to use ALINES without using STRTRAN, because you can pass the delimiter character as a parameter.

I did make a typo.... I meant:
nCount = ALINES( myArray, STRTRAN(pUser,',',chr(13)) )
not
nCount = ALINES( myArray, STRCOUNT(pUser,',',chr(13)) )

... the discussion of OCCURS() had gave me 'count-on-the-brain'
 
Ah,
I read you're ALINES() syntax wrong - I "saw" four parameters and knew it had to be 7/8. I guess it's time to read a bit slower!

Rick
 
I'm still working in VFP6, so I've made extensive use of the STRTRAN fudge.... But I just found out my boss ordered VFP8...[thumbsup2]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top