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!

Tidying Code 2

Status
Not open for further replies.

audiopro

Programmer
Apr 1, 2004
3,165
GB
I am tidying code again and wondered if there was an easy way to tidy this up.
I would rather not select * as listing the fields makes it more readable.
The part I would like to tidy up is the declaration of the 'L' var.
I could do it in a loop if the fields were all the same but there is a mixture of text and integers in there so the format code for the items will differ.
Code:
SELECT TABLENAME,FEELD,CAPSHUN,FIELDTYPE,SHOWTYPE,REQDTYPE,FFORMAT,FCHECK FROM CAPTIONS INTO ARRAY CAPLIST
C=FCREATE(CAPNAME,0)
FOR X = 1 TO ALEN(CAPLIST) STEP 8
	L = ALLTRIM(CAPLIST[x])+","+ALLTRIM(CAPLIST[x+1])+","+ALLTRIM(CAPLIST[x+2])+","+ALLTRIM(STR(CAPLIST[x+3]))+","+ALLTRIM(STR(CAPLIST[x+4]))+","+ALLTRIM(STR(CAPLIST[x+5]))+","+ALLTRIM(STR(CAPLIST[x+6]))+","+ALLTRIM(STR(CAPLIST[x+7]))+","+CHR(13)+CHR(10)
	W=FWRITE(C,L)
NEXT
CL = FCLOSE(C)

Keith
 
Hi

I believe you have to explain a little bit what you are doing here, c.q. what you expect to have as result.
It does not look like default VFP coding.

Code:
SELECT TABLENAME,FEELD,CAPSHUN,FIELDTYPE,SHOWTYPE,REQDTYPE,FFORMAT,FCHECK FROM CAPTIONS INTO ARRAY CAPLIST
C=FCREATE(CAPNAME,0)
FOR X = 1 TO ALEN(CAPLIST) STEP 8
	L = ALLTRIM(CAPLIST[x])+",";
+ALLTRIM(CAPLIST[x+1])+",";
+ALLTRIM(CAPLIST[x+2])+",";
+ALLTRIM(STR(CAPLIST[x+3]))+",";
+ALLTRIM(STR(CAPLIST[x+4]))+",";
+ALLTRIM(STR(CAPLIST[x+5]))+",";
+ALLTRIM(STR(CAPLIST[x+6]))+",";
+ALLTRIM(STR(CAPLIST[x+7]))+",";
+CHR(13)+CHR(10)
	W=FWRITE(C,L)
NEXT
CL = FCLOSE(C)

Regards,

Jockey(2)
 
I agree. You need to tell us what the end product is.

If I've understood your code correctly, it looks like you are starting with a table named Captions, and you want to end up with a text file containing the contents of that table, one record per line, separated by commas. If that's right, then have you considered simply copying the file? Something like:

Code:
SELECT Captions.
COPY TO Capname TYPE DELIMITED

or some variation thereof.

If I have misunderstood your aim, perhaps you could explain it further.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Just mentioning: A query INTO ARRAY generates a 2D array. No need for your step 8, if you address elements with [row,col] instead of a single index [x].

Maybe you just COPY TO TYPE DELIMITED, as Mike said. You won't need your code at all, if that gives the desired result.

Bye, Olaf.



 
But, just in case I was wrong and there's more to it than a simple COPY command, I'll also answer your original point.

You said:

I could do it in a loop if the fields were all the same but there is a mixture of text and integers in there so the format code for the items will differ.

The solution to that is to use the TRANSFORM() function. In general, that's the function you need for converting any data type to a character string (even if you don't know in advance what data types you are working with).

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
You might like TextMerge(), too:

L=""
Select CAPTIONS
Scan
L = L + Textmerge("<<TABLENAME>>,<<FEELD>>,<<CAPSHUN>>,<<FIELDTYPE>>,<<SHOWTYPE>>,<<REQDTYPE>>,<<FFORMAT>>,<<FCHECK>>")+chr(13)+chr(10)
Endscan

Bye, Olaf.
 
Sorry, in CODE tags:

Code:
L=""
Select CAPTIONS
Scan
   L = L + Textmerge("<<TABLENAME>>,<<FEELD>>,<<CAPSHUN>>,<<FIELDTYPE>>,<<SHOWTYPE>>,<<REQDTYPE>>,<<FFORMAT>>,<<FCHECK>>")+chr(13)+chr(10)
Endscan 
StrToFile(L,CAPNAME)

Bye, Olaf.
 
i do not see any more room for improvement in Olaf' 6-lines of code. if you move first and last line inside the scan loop, and initialize the file before the loop starts like,

StrToFile('',CAPNAME,0)

that is 7 lines total. the file can grow to 2gb.

nasib
 
Thanks guys.
Learnt some things there so worth asking the question.
The code itself grew out of nothing as it was part of a development. Now the final app is working, the code needed tidying up. I don't know if that is the way you guys develop code but it works for me.
I tried Copy to initially but it only seems to put the file into the default dir, despite the name having a full path. It has to go in a specific DIR.
I don't know why I didn't consider transform as I am using that already in the OOP code.
Thanks for the heads up on ALEN, never noticed it had additional params.
Textmerge() gave an error when I tried it.
I just wanted to make the code more readable and in the end I have settled for.

Code:
SELECT TABLENAME,FEELD,CAPSHUN,FIELDTYPE,SHOWTYPE,REQDTYPE,FFORMAT,FCHECK FROM CAPTIONS INTO ARRAY CAPLIST
C=FCREATE(CAPNAME,0)
FOR X = 1 TO ALEN(CAPLIST,1)
	L=''
	FOR Y=1 TO ALEN(CAPLIST,2)
		L=L+ALLTRIM(TRANSFORM(CAPLIST[x,y]))+','
	NEXT
	L=L+CHR(13)+CHR(10)
	W=FWRITE(C,L)
NEXT
CL = FCLOSE(C)

Keith
 
Glad you've got it working, Keith. But just to pick up a couple of your points:

I tried Copy to initially but it only seems to put the file into the default dir, despite the name having a full path

Are you sure about that? I've never had any difficulty in getting it to store the file in a specified directory. I'm sure it works.

The code itself grew out of nothing as it was part of a development. Now the final app is working, the code needed tidying up. I don't know if that is the way you guys develop code but it works for me.

If it works for you, that's the main thing. Me, I was brought up to do the opposite. Develop each component separately; make it as clean and simple as possible from the outset; test it to destruction. Then forget about it. But each to his own style of working.

Mike




__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Are you sure you use VFP9, Textmerge() as function is new. But Textmerge capabilities also are present in VFP6 with TEXT TO.. ENDTEXT, since VFP7 you can TEXT TO Variable, in VFP6 you have to Text to a file, not a handicap in your case.

Aside of that COPY TO does write to a fully qualified path. What did you try?

COPY TO CAPNAME will create a capname.dbf in the current dir, obviously.
COPY TO (CAPNAME) will write to the file name (including path) given in the variable.

That's the disadvantage of commands with command options, accepting file names without quotation marks. VFP will not look for a variable named CAPNAME, if it can assume you mean that as file name, as the command syntax allows that. This is nothing new, also valid for more common commands as USE, also USE CAPNAME would search for capname.dbf in the current folder. The same goes for COPY FILE and more.

Even if not knowing ALEN(..,2) for number of columns, you know that number, you could also have written:
Code:
	FOR Y=1 TO 8
		L=L+ALLTRIM(TRANSFORM(CAPLIST[x,y]))+','
	NEXT
It's of course cleaner to use ALEN to get the column count.

Bye, Olaf.
 
I always thought you already were at VFP9, or does it depend on a project by project basis?

The compatibility of VFP is very nice, so it would be easy to migrate, all you need to roll out additional to your EXE is the newer runtimes.

I don't rmember how far VFP6 has differences in COPY TO, but it should do in thet version, too. You still could replace all your code with just that command and that would be worth it, so pardon me pushing that again and again.

The more verbose syntax is COPY TO (CAPNAME) FIELDS TABLENAME,FEELD,CAPSHUN,FIELDTYPE,SHOWTYPE,REQDTYPE,FFORMAT,FCHECK TYPE DELIMITED WITH '"' WITH CHARACTER ','

double quotation markes are the default character value delimiter though, as they might contain commas in them, and comma is the default field separator character.

Also, this does not generate a final comma after the last field as you do, that normally is obsolete, I woner if that is important in your case, you would just need an additional empty field and you get that extra comma.

Bye, Olaf.
 
I don't rmember how far VFP6 has differences in COPY TO, but it should do in thet version, too. You still could replace all your code with just that command and that would be worth it, so pardon me pushing that again and again.

According to HackFox, the main differences in VFP 6.0 concerned copying to spreadsheet formats, such as WRK. There are no reports of any restrictions in copying to a text file. In addition, I'm sure I used to copy to text files in remote directories successfully using FPD and even FoxPlus, so I can't imagine VFP 6.0 not handling it.

Keith, I know you have found a solution to your original question, but I think you should persevere with the COPY TO solution again, if only for future benefit.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Using COPY TO seemed a neater solution so I returned to it and had another go.
Using the basic COPY TO (CAPNAME) did indeed create a file in the correct directory, not sure why that didn't work yesterday.
The file created was similar to a copy structure result where it puts all the table data in the file as well, not what I wanted.
The more verbose syntax, suggested by Olaf did the job properly but of course includes speech marks in the file.
Code:
COPY TO (CAPNAME) FIELDS TABLENAME,FEELD,CAPSHUN,FIELDTYPE,SHOWTYPE,REQDTYPE,FFORMAT,FCHECK TYPE DELIMITED WITH '"' WITH CHARACTER ','
The resulting file is then auto Ftp'd to my remote server where the speech marks are easily removed and the file works a treat.
Thanks for the pointers.

Keith
 
The file created was similar to a copy structure result where it puts all the table data in the file as well, not what I wanted.

What do you mean by that? Do you mean that the file included the field names in the first row? If so, that's what you get if you do [tt]COPY TO ... TYPE CSV[/tt]. You don't get that with [tt]COPY TO .... TYPE DELIMITED[/tt].

The basic form of [tt]COPY TO .... TYPE DELIMITED[/tt] gives you just the data from the original table, and nothing else. Each field is terminated by a comma, and character fields are enclosed in double quotes. If you don't want the double quotes, you can do this:

[tt]COPY TO c:\work\junk.txt TYPE DELIMITED WITH "" WITH CHARACTER ","[/tt]

There are many other variations of COPY TO, as I indicated in my first post in this thread.

Mike




__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Thanks Mike
Getting confused with another file on there, the one with the junk in was created during my original attempts to get COPY TO working.

I tried various variations of
Code:
COPY TO c:\work\junk.txt TYPE DELIMITED WITH "" WITH CHARACTER ","
but they error when I try to save the file.
It isn't a problem, the previous code I posted works fine.

Keith
 
Keith, if you're getting an error when it tries to save the file, that might be because a previous copy of the file is already open - in a text editor, for example. Or perhaps because the directory (c:\work in my example) doesn't exist.

I know you have a working solution, but it might be worth a few moments of your time to double-check the above points.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
I delete the existing file before creating a new one to avoid the messagebox.
I will give it a try when I have a bit of time, need to press on with it now.

Keith
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top