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

Understanding VFP Functions 2

Status
Not open for further replies.

T17Rax

Technical User
Jun 11, 2015
39
GB
Morning All,

I feel a complete idiot when asking on here (because I'm a complete novice!) but I'm wondering if anyone can help me on understanding functions (or rather a particular funtion).

I decided to write a program and with some great help, I got a solution out of my error(s) in that program. However, it was written in code I don't understand.
I've been studying the code as much as I can, using google and MSDN to get me through and so far it's been good up to now.

However, here's the part I can't get my head around:

Code:
Select Cardno from id where reusable into array laPins
Messagebox('Deleted: ' + transform(alen(laPins,1)))

What does transform do?
The code here displays pin numbers in a messagebox that I'm after from a field "Cardno" and reusable is a logical field. The pin numbers are in a character based format.
According to other websites, transform literally transforms characters or numerics or etc but I don't understand where, how or why you would use them. Would someone be so kind to explain this?
I understand that alen describes an array and has to have a numeric value to indicate either rows or columns..

Any help is much appreciated.

Kind Regards,
Vibrantseeker
 
Hello again Vibrant,

First, I must put you right on one important point. You wrote: "I feel a complete idiot when asking on here (because I'm a complete novice!)".

There is absolutely no need to feel like that. We are all novices here - or were at some point in the past. And, speaking personally, I often feel like a complete idiot, even after many years as a programmer. So never feel bad about posting questions - however elementary. As someone once said: "The only stupid question is the one you don't ask."

So, regarding the TRANSFORM() function. In short, it converts any data type to a character string. If you pass it a number, it will return that number in character format. If you pass it a date, it will return the date expressed as DD/MM/YY (or MM/DD/YY, or whatever other format is stipulated by SET DATE). And so on.

In general, you don't need to use it with a message box, because the MESSAGEBOX() functions automatically converts whatever you give it to a character string. But TRANSFORM() is useful in many other cases.

But in this case, you do need to use it, because you are concatenating a number with some text (the word "Deleted:"). The ALEN() function returns a number. If you just had [tt]'Deleted: ' + alen(laPins,1)[/tt], you would get an error because you would be trying to add a number to a string. TRANSFORM() solves that problem.

Next point: It's not true that ALEN() "describes an array". What ALEN() does is return the size of an array. The fact that your second parameter is the number 1 means that you want it to return the number of rows in the array. So what your code is doing is displaying the number of records in your Cardno table where the Reusable field is set to .T.

Does that help at all?

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Hi there Mike,

Thanks for your explanation. It does seem a lot clearer when someone else like yourself is able to explain it! I think the trouble with sites like MSDN is that they expect you to know the background knowledge with VFP and I'm still covering that ground as well as everything else.

Thanks tbleken for the link. I'll be sure to give that a read over!


Thanks again,
Vibrantseeker.
 
Just one more point:
The Messagebox displays the number of resuable cardnos, which is the number of array rows the previous SQL-Select puts into the laPins array.
As you now don't delete your records anymore, but set the reusable field to .T., you should refine that text to say "Reusable:" instead of "Deleted:". Maybe that also helps you see what it does.


Besides that:
Instead of [pre]'Reusable: ' + transform(alen(laPins,1)))[/pre] you could also display [pre]'Reusable: ' + transform(_tally)[/pre] Because the number of rows is from the last SQL query, which ran and _tally gives the result row count, too, not only for SQL-Selects.

And if you'd not select into an ARRAY but into a CURSOR instead, you could also use [pre]transform(reccount())[/pre].

Last not least, a cursor is easier to handle than an array, eg you can simply BROWSE a cursor, which you can't with an array. The simplest way to "browse" an array is to start the debugger and look at the "Locals" Window or "Watch" Windows, which will show arrays as a treeview you can expand to see the elements. Not as nice as a browse shows records, though.

Bye, Olaf.
 
Thanks Olaf for the heads up on that one! :)

Vibrant.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top