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!

Something for the weekend #2: Numbering the lines 3

Status
Not open for further replies.

Mike Lewis

Programmer
Jan 10, 2003
17,516
Scotland
My previous weekend brain exercise turned turned out to be too easy - although it did generate an interesting discussion. Let me give you something different to think about this time.

You have a text file. The requirement is simply to add line numbers to it.

For example, the file might look like this:

[tt]Alfreds Futterkiste
Antonio Moreno Taquería
Around the Horn
Berglunds snabbköpö
Blauer See & Delikatessen
Blondel père et fils
Bólido Comidas preparadas
Bon app' edited
Bottom-Dollar Markets[/tt]

Your job is to make it look something like this:

[tt]1 Alfreds Futterkiste
2 Antonio Moreno Taquería
3 Around the Horn
4 Berglunds snabbköpö
5 Blauer See & Delikatessen
6 Blondel père et fils
7 Bólido Comidas preparadas
8 Bon app' edited
9 Bottom-Dollar Markets[/tt]

As before, the aim is to do the job with the least amount of code (fewest lines, shortest lines).

If you are one of the first to reply, please wrap your solution in [ignore][/ignore] tags so that others won't see it straight away.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
If I understood correctly, I have three line code but Spoiler does not show code to me

CREATE CURSOR mCursor (Fld1 c(50))
APPEND FROM simple.txt TYPE SDF
SELECT STR(RECNO(),2) as Sr,Fld1 as Name FROM mCursor TO FILE x.txt
 
So far I need more code. Haven't looked at newtofoxpros code yet.

Code:
Cd H:\testSet Textmerge To output.txt
Set Textmerge On
For i = 1 To ALines(a,FileToStr("input.txt"))
\\<<i>> <<a[i]>>
EndFor
What's missing to make this more comfortable is SET CONSOLE OFF, SET SAFETY OFF and a final SET TEXTMERGE TO

Bye, Olaf.
 
NewToFoxpro and Olaf,

Good answers from both of you. But both are too verbose. It is possible to do it with less code.

NewToFoxpro: Keep in mind that the line numbers might exceed two characters. Also, it would be nice to have a space between the line number and the text.

As always, your code should restore the environment to what it was before. So if you change any settings, you much change them back. However, you don't need to worry about SET SAFETY, and (as per Olaf's comment), don't worry about SET CONSOLE (so it doesn't matter if the results are echoed to the screen).

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Another try.
This time old vs new shortened to 4 char commands/function names, where applicable. CD would need to be prepended to each variant, of course, but we might simply make the assumption to work in the current directory and skip that line.
Code:
Cd H:\test
Set Text To output1.txt
Set Text On
For i = 1 To ALin(a,FileT("input.txt"))
\\<<i>> <<a[i]>>
Next

crea curs cc (l c(30))
appe from input.txt type sdf
set fiel glob
set fiel to n=tran(recn())+' ',l
copy to output2.txt type sdf
The first part needs less chars, the second code makes elegant use of SET FIELDS, nevertheless lines are padded to 30 chars by C(30), that also happens with varchar V(30)

Bye, Olaf.
 
Seems like a silly thing to do in general. [glasses]
create cursor numbers (field1 c(100))
append from input.txt sdf
list to file output.txt
use
 
Dan is definitely heading in the right direction. But the code is still too long.

You should be able to do it without an intermediate cursor - just using VFP's text file handling features.

By the way, the solution I have in mind should work in any recent version of VFP - at least back to 6.0. And it does not rely on any undocumented features.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Well, then how about

Set Alte To output4
Set Alte On
For i = 1 To ALin(a,FileT("input.txt"))
?i,a
Next
Set Alte Off
Set Alte To

You have to mean something else, though, as this is not really considerably shorter.
Bye, Olaf.
 
SET HEADINGS OFF
TYPE myfile.txt NUMBER TO mynumberedfile.txt

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
For lnCount = 1 to Alines(laArray,Filetostr('input.txt'),CRLF)
StrToFile( Transform(lnCount) + '. ' + laArray[lnCount], 'output.txt', .t.)
Endfor
 
Kudos to Vilhelm-Ion. This is very close to the answer I had in mind.

A couple of minor points:

- In my original post, I said " ... the file might look like this:" and "Your job is to make it look something like this:". This implies that you were required to change the original file, rather than create a new one. But that's OK. You just use the same filename for both the input and output file (and I did say you can ignore SET SAFETY).

- The solution I had in mind was just a single command, this being the second line in Vilhelm-Ion's solution. I didn't include the SET command. The presence or absence of the SET command does slightly alter the content of the output file, but I didn't think that significant. Of course, if you do change the setting in this way, you would need to change it back again.

I doubt we can improve Vilhelm-Ion's solution (but feel free to try). But some of your other attempts were ingenious - I especially like Dan's last shot.

I hope you all enjoy doing these little exercises. I've got one or two others in mind, but I'll save them for a slack period.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Mike,

[tt]TYPE[/tt] is available at least as far back as FoxPro 2.6 as it appears in my FP2.6 Language Reference book and appears to work exactly the same way.

mmerlinn


Poor people do not hire employees. If you soak the rich, who are you going to work for?

"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond
 
Mike,

yes these things are fun, not too time consuming and still you read up the one or other thing. I came across SET HEADINGS just after dan posted his first LIST solution, because of the headings LIST gave me. Never needed this, as I never used LIST or TYPE.

Anyway, I could have used StrToFile in it's append mode just as dan in his last solution, that needs less than all my attemps with textmerge or alternate.

TYPE is something I remember faintly from DOS and yes, it's still a command in the windows command shell. It just doesn't have the options and clauses VFP offers. If I'd like to see line numbers I load a file into notepad++, you may have another favorite text file editor.

Bye, Olaf.
 
TYPE is available at least as far back as FoxPro 2.6

Thank you for that, Mr Merlinn. You are correct. But did it have the NUMBER clause back then? That clause wasn't present in Foxbase. It was present in dBASE IV, but the TO FILE clause wasn't.

Regarding the TO FILE clause, HackFox has this to say:

Why you'd want to scroll one file to another is a mystery to us

Well, precisely in order to number the lines - should you even want to.


Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
>to scroll one file to another
Well, if you simply look at it as file copy there is copy for that. Linux shell commands are all implementing the stdin/stdot paradigm and so you can pipe many things into others and create processes the designers of the single commands didn't even thought of. And a typical last output channel is a file or printer.

Bye, Olaf.



 
Mike,

In FP2.6 [tt]TYPE[/tt] has the following definition:

[tt]TYPE<file1>[/tt]
[tt][AUTO][/tt]
[tt][WRAP][/tt]
[tt][TO PRINTER[PRINTER]|TO FILE<file2>][/tt]
[tt][NUMBER][/tt]​

mmerlinn


Poor people do not hire employees. If you soak the rich, who are you going to work for?

"We've found by experience that people who are careless and sloppy writers are usually also careless and sloppy at thinking and coding. Answering questions for careless and sloppy thinkers is not rewarding." - Eric Raymond
 
Hi Mike

You know that won't produce exactly the output you specified, the numbers are left padded...
1 Alfreds Futterkiste
2 Antonio Moreno Taquería

B-)

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are not good for you.
 
Ah, perhaps you meant them to be padded and TT took it away on posting...

Regards

Griff
Keep [Smile]ing

There are 10 kinds of people in the world, those who understand binary and those who don't.

I'm trying to cut down on the use of shrieks (exclamation marks), I'm told they are not good for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top