This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Lines in file

Old posts that have not been replied to for several years.
Locked
d
digitaldj
Voice
Posts: 23
Joined: Tue Jan 07, 2003 9:07 am

Lines in file

Post by digitaldj »

how can i find out how many lines there is in a file? :wink:
User avatar
caesar
Mint Rubber
Posts: 3777
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Open the file, then use a foreach to increment a number.. Then at the end just extract the value.

Example of a foreach and incrementation of a value:

set value 0
foreach line $file {
incr value
}

Also you may want to see this page for the 'foreach' and this one for the 'open' command..
Once the game is over, the king and the pawn go back in the same box.
d
digitaldj
Voice
Posts: 23
Joined: Tue Jan 07, 2003 9:07 am

Post by digitaldj »

ok.. i knew that, but i thought it might be something more simple..
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

The foreach command, loops through each line in a list.

In other words, you have to create a list with the contents of a file. One element for each line.

For this, you might as well use the "llength" command, which will count for you.

As noted, you need the list in the first place, as they don't create themselves.

Use somthing like

Code: Select all

set lines [llength [split [read [set fp [open "FILE.NAME" w]]] "\n"]]
close $fp
The $lines variable should now contain the number of lines.
User avatar
caesar
Mint Rubber
Posts: 3777
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Well, not all the things are simple as they look like.. The usage of the 'foreach' thing is logical to be used. I don't have any ideea (at this moment) of another way to do the same thing. Combining mine and ppslim's reply you should solve this in a sec.. Heh..
Once the game is over, the king and the pawn go back in the same box.
d
digitaldj
Voice
Posts: 23
Joined: Tue Jan 07, 2003 9:07 am

Post by digitaldj »

well, i just thought it maybe was a single command for it or something.. like in mIRC it's just $lines(file.txt)
e
egghead
Master
Posts: 481
Joined: Mon Oct 29, 2001 8:00 pm
Contact:

Post by egghead »

caesar wrote:Well, not all the things are simple as they look like.. The usage of the 'foreach' thing is logical to be used. I don't have any ideea (at this moment) of another way to do the same thing. Combining mine and ppslim's reply you should solve this in a sec.. Heh..
Another way is to use OS utilities like "wc" (wordcount). A while loop with an increasing counter usually is not that efficient in terms of CPU usage although memory needs are low.

For smaller files the script given by ppslim (which contains some errors, finding and solving them are left as a trivial exercise) works ok. For larger files [exec] in combination with"wordcount" works good.
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

So it does.

The w in the open command should be a r.

However, you said errors, and not error. Can you point any more out?
User avatar
caesar
Mint Rubber
Posts: 3777
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

I have said that at that moment I didn't knew another alternative. I havent heard about the "wc" (wordcount) until now. ppslim's advice from changing the w (write) to r (read) seems corectly. There is no need to write, just to read from the file.

Also I don't see where this "wc" (wordcount) can be used on the *lines* count, not words count..
Once the game is over, the king and the pawn go back in the same box.
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

My advice, is whenever you see a new command introduced, try looking in the Tcl manual, or doing "man <command>" on the shell.
wc --help wrote: Usage: wc [OPTION]... [FILE]...
Print line, word, and byte counts for each FILE, and a total line if
more than one FILE is specified. With no FILE, or when FILE is -,
read standard input.
e
egghead
Master
Posts: 481
Joined: Mon Oct 29, 2001 8:00 pm
Contact:

Post by egghead »

ppslim wrote:So it does.

The w in the open command should be a r.

However, you said errors, and not error. Can you point any more out?
Create a one line file and run the script.
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Very true.

Then the code should now read

Code: Select all

set file "FILE.NAME"
set lines [llength [split [read -nonewline [set fp [open $file r]]] "\n"]]
close $fp
I forget that splitting at the newline, causes a extra blank element.

Read will leave a newline at the end, because of the nature of files, causing Tcl to think of a new element on split.
User avatar
caesar
Mint Rubber
Posts: 3777
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Probably I havent seen it in the manual. Thanks for pointing it out..
Once the game is over, the king and the pawn go back in the same box.
Locked