Forum Moderators: coopster

Message Too Old, No Replies

Return a line number as a number?

Search asdf.txt for certain number of instances and return that line number

         

IamStang

1:09 am on Aug 10, 2005 (gmt 0)

10+ Year Member



OK, I have a txt file set up someting like this.

10001¦1234¦56789¦1
10001¦1234¦56789¦1
10001¦1234¦56789¦2
10001¦1234¦56789¦1
10001¦1234¦56789¦3
10001¦1234¦56789¦1
10001¦1234¦56789¦4
10001¦1234¦56789¦1

Now, keep in mind this file is actually several hundred lines long. Is it possible to search this file for the third instance of ¦1\n and ,as it appears above, return the number 4 as a variable?

Any help/thoughts are welcomed.

yktan

2:22 am on Aug 10, 2005 (gmt 0)

10+ Year Member



Yes you can. You can read the text file line by line (and therefore record down the line number) and scan for the last number. But if it's a big text file, it will take a long time to traverse the file. If it's possible, I suggest that you create a table and store the values there. It's much easier and faster to use a SQL query for your purpose.

IamStang

11:33 am on Aug 10, 2005 (gmt 0)

10+ Year Member



Thanks for your input,

At this time it is not possible to use SQL. I have not installed it on this machine. The project I am working on now is merely for learning and wait time is not an issue at the moment.

I want to get a pretty good grasp of PHP prior to diving into SQL.

Looking through my documentation, I am not seeing a function that returns the current line number as a number. And do I need to explode the file first?

Tvienti

4:31 pm on Aug 10, 2005 (gmt 0)

10+ Year Member



You would need to keep track of the current line yourself. If all of the lines are a fixed length, which they appear to the in your example, you could just fread them in one line at a time, incrementing some counter on each iteration. When you find what you're looking for, break out of the loop and your counter will be your line number.

IamStang

11:06 pm on Aug 11, 2005 (gmt 0)

10+ Year Member



Thanks for your ideas and thoughts.