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

Array question 1

Status
Not open for further replies.

linousa

IS-IT--Management
Mar 8, 2013
79
US
Is it possible to use conditional statement in ASCAN? Need to search array for values over 5:
=ascan(myArr, =>5)
 
No.

The problem here is that Ascan() expects an expression in the second parameter and ">=5" is only half of an expression. Unfortunately, there's no way to know which element is currently being scanned so there's no way to complete the expression.

You could do your own fairly easily. Off the top of my head:

Code:
Lparameters taArray
* Process the array element by element
For lnCount = 1 to Alen(taArray)
   If Type(taArrray[lnCount]) = "N" And ;
      taArray[lnCount] >= 5
      Return lnCount
   Endif
Endfor
Endfor

And now let the syntax battles begin. You could use FOR EACH, etc.

As I said, it's off the top of my head.
 
1 You can use a FOR loop
2 If your goal is to check if there exists a value >5, you can sort the array with ASORT() and compare the first/last element with 5
3 With a little trick, you can use ASCAN
- create a new array with the signs of the original array - 5 and ASCAN() the second array for 1
Code:
LOCAL myArr[5]
LOCAL la[ALEN(myArr)]
myArr[1] =4
myArr[2] =3
myArr[3] =8
myArr[4] =1
myArr[5] =6
FOR lni = 1 TO ALEN(la)
	la[lni] = SIGN(myArr[lni]-5)
NEXT
?ASCAN(la,1)

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
Today I'm absent-minded. Now I saw the equal sign
Code:
LOCAL myArr[5]
LOCAL la[ALEN(myArr)]
myArr[1] =4
myArr[2] =3
myArr[3] =5
myArr[4] =1
myArr[5] =6
FOR lni = 1 TO ALEN(la)
	la[lni] = MIN(SIGN(myArr[lni]-5),0)
NEXT
?ASCAN(la,0)


Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
How do you create the array? If you query INTO ARRAY you could easily include the condition within the query where clause and never get the elements in the array, which you don't want.
And overall cursors are superior to arrays in VFP. Besides some situations I only find arrays useful, which result from Axyz functions, eg AFIELDS, ALINES, ADBOBJECTS, etc.

Bye, Olaf.
 
Used Olaf's suggestion, fixed my query, but thank you everyone it was very helpful to see different solutions. What is cursor in VFP?
 
A cursor is a temporary table. It exists only for as long as it is open, and disappears when you close it.

A very common technique in VFP is to use a SQL SELECT to extract data that meets some condition. Olaf's point was that if you happened to be using SELECT to create your array, you could add a WHERE clause to the SELECT to obtain the records where the value was greater than or equal to 5, and so you wouldn't need to search the array for those values.

He then went on to say that, rather than using an array in that case, you might as well use a cursor. SELECT can send its results to a cursor just as it can to an array, and cursors have several other advantages.

Does that help?

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Yes, Mike explained that very well. INTO CURSOR name is as valid as INTO ARRAY name, so it wouldn't be a big change to your code. Like arrays vanish from memory, if you're done with them or the method, so cursors vanish with a USE. And you can query from cursors, index cursors, bind controls to cursors. More than you can do with arrays.

Bye, Olaf.
 
What is cursor in VFP?

Mike and Olaf explained it well, but let's note that a cursor is not a VFP-specific concept. It's actually a mnemonic that comes from IBM: CURrent Set Of Records.

You can SELECT ... INTO CURSOR, or you can CREATE CURSOR. They're very useful but all they are is a set of records. They behave just like an opened DBF (unless they're read-only) but they go away when you close them. (No more cleaning up temp files!)

Very useful. It would pay you back to do some fiddling to figure this beast out.
 
Mike, Olaf And Dan thank you so much for your help, it was very helpful, now it is time for me to "play" more with SELECT... Once again, I very appreciate your help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top