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.

While....

Old posts that have not been replied to for several years.
Locked
B
BrollY
Voice
Posts: 36
Joined: Wed Apr 23, 2003 9:28 pm
Location: H07 L471N4 L4ND (Puerto Rico)

While....

Post by BrollY »

so far, in all my TCLs i use the 'foreach' function to read files n such, though ive come to scripting something that i believe that 'foreach' isnt doing the job properly. i tried to use the 'while' function to read files but i cant seem to get it to work how i want it to. how would i go about making it read a file, n how would i check each line? like in 'foreach' i would just call each line $line (foreach line $var {)
Yo momma's so fat she can be in the past, present, n future all at once
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Code: Select all

set f [open file r]
while {![eof $f]} {
    gets $f line
    # here's your $line...do what ever you like with it
}
close $f
in other words...

while {end of file is NOT reached} {get one line from the file and store the value in a variable called 'line'}
User avatar
stdragon
Owner
Posts: 959
Joined: Sun Sep 23, 2001 8:00 pm
Contact:

Post by stdragon »

In most circumstances, foreach is the best way. Using a while loop is more cumbersome because you have to check for eof after you read, not before. The code User posted doesn't do this, so you will end up processing an extra blank line in each file. The correct while loop code is like this:

Code: Select all

set file [open sheep/baa.txt r]
while {[gets $file line] != -1} {
  # $line holds the line read
}
close $file
Because Tcl is rather inefficient, if you are reading many lines it's much better to use foreach line [split [read $file] \n] like that. That makes tcl read all the lines at once using C code. For long files, it is *much* faster.
B
BrollY
Voice
Posts: 36
Joined: Wed Apr 23, 2003 9:28 pm
Location: H07 L471N4 L4ND (Puerto Rico)

Post by BrollY »

im aware foreach is the better option n wat while does, but since foreach isnt properly doing wat i want, i gotta try the other choice
Yo momma's so fat she can be in the past, present, n future all at once
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

what exactly is it that foreach isn't doing properly?
B
BrollY
Voice
Posts: 36
Joined: Wed Apr 23, 2003 9:28 pm
Location: H07 L471N4 L4ND (Puerto Rico)

Post by BrollY »

its really hard to explain the real point of the tcl if u dont play a certain game, but ill give it a try.... somehow...

i just checked with some 'putlog' things to see if the bot gets stuck somewhere or something, n well, it reads a file it dls from a site, n with a command it searches the given parameters within the file. apparently, its not reading the first half of the file. i tried it with the 'while' function too, same result.
Yo momma's so fat she can be in the past, present, n future all at once
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

This sounds weird. Have you verified that the file actually contains more data than what you're able to read/gets from it?
B
BrollY
Voice
Posts: 36
Joined: Wed Apr 23, 2003 9:28 pm
Location: H07 L471N4 L4ND (Puerto Rico)

Post by BrollY »

ofc, downloaded it quite a few times already, i open it with wordpad over n over again n i can see the entire file n its just fine, but the bot itself wont read the first half
Yo momma's so fat she can be in the past, present, n future all at once
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Try 'fconfigure <file channel> -translation binary' before you read/gets from it.
B
BrollY
Voice
Posts: 36
Joined: Wed Apr 23, 2003 9:28 pm
Location: H07 L471N4 L4ND (Puerto Rico)

Post by BrollY »

ehhhhh how would i go about doing that? :-? :wink:
Yo momma's so fat she can be in the past, present, n future all at once
User avatar
user
&nbsp;
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

something like

Code: Select all

set f [open file];# r is the default mode, so it's not needed
fconfigure $f -translation binary
set lines [split [read $f] \n]
...
B
BrollY
Voice
Posts: 36
Joined: Wed Apr 23, 2003 9:28 pm
Location: H07 L471N4 L4ND (Puerto Rico)

Post by BrollY »

ok, tried it with the fconfigure thing n without it, n kept the putlog thing. the result is that it seems it does read the entire file, but it only starts doing the putlog thing after half the file has been read.

i honestly dont know wats wrong, n ive changed the coding many many many times over the past 4 days.
Yo momma's so fat she can be in the past, present, n future all at once
B
BrollY
Voice
Posts: 36
Joined: Wed Apr 23, 2003 9:28 pm
Location: H07 L471N4 L4ND (Puerto Rico)

Post by BrollY »

nevermind, problem fixed
but thanks for the help
Yo momma's so fat she can be in the past, present, n future all at once
Locked