Register to post in forums, or Log in to your existing account
 

Play RetroMUD
Post new topic  Reply to topic     Home » Forums » zMUD General Discussion
JMinara
Beginner


Joined: 05 Jul 2005
Posts: 14

PostPosted: Thu Apr 20, 2006 6:59 am   

Determining line number of a string in a text file
 
I'm working with a textfile and I'm searching for a string, and I need to know the line number of that string. Basically I need %grep, except instead of returning the value of the line I need it to return the line number.

Is there any way to do that?

Edit: Evidently not.
Reply with quote
shalimar
GURU


Joined: 04 Aug 2002
Posts: 4689
Location: Pensacola, FL, USA

PostPosted: Thu Apr 20, 2006 7:19 am   
 
if you can loop through the file line by line, adding them in turn to a variable array... %ismember would return the numeric position within the array
_________________
Discord: Shalimarwildcat
Reply with quote
Tarn
GURU


Joined: 10 Oct 2000
Posts: 873
Location: USA

PostPosted: Thu Apr 20, 2006 7:20 am   Re: Determining line number of a string in a text file
 
JMinara wrote:
I'm working with a textfile and I'm searching for a string, and I need to know the line number of that string. Basically I need %grep, except instead of returning the value of the line I need it to return the line number.

Is there any way to do that?

Edit: Evidently not.


Why not?

Open a file with #FILE.

You probably have a readme.txt in your zMud directory, so

#FILE 1 "readme.txt"
(opening "readme.txt" as file 1)
Then,
#SHOW %read(1)
will read a single line from file 1.

Instead of showing the line, put it in a loop with a counter, and use the "~=" pattern-checking operator to see if a particular line matches a pattern. If it matches the pattern, the counter can tell you what line you're on.

Alternatively: use the mechanism above to read the file and write another, numbering the lines as it goes. %Then grep could give you the numbered lines matching your pattern from the new file.

-Tarn
Reply with quote
Display posts from previous:   
Post new topic   Reply to topic     Home » Forums » zMUD General Discussion All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

© 2009 Zugg Software. Hosted by Wolfpaw.net