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.
Help for those learning Tcl or writing their own scripts.
romain
Voice
Posts: 12 Joined: Sun Oct 16, 2005 1:51 am
Post
by romain » Fri Dec 09, 2005 4:58 am
hello,
I've 2 files(file1,file2),have the same number of lines,30 lines max.I want to write data which are in the file1 but which is not in the file2.I try use foreach 2 time but I've many difficulties to write these data.Can you help me in the realization of this script?Thx
Sir_Fz
Revered One
Posts: 3794 Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:
Post
by Sir_Fz » Fri Dec 09, 2005 7:04 am
You can take the line from file1 and check if it exists in file2, if not then write it to the file and repeat that on each line.
romain
Voice
Posts: 12 Joined: Sun Oct 16, 2005 1:51 am
Post
by romain » Fri Dec 09, 2005 8:33 am
Yes but,
with
Code: Select all
foreach a [split [read [open $file1] \n] {
foreach b [split [read [open $file2] \n] {
if [string equal $a $b] {
continue
} else {
lappend c $a
}
}
}
Don't works
Sir_Fz
Revered One
Posts: 3794 Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:
Post
by Sir_Fz » Fri Dec 09, 2005 10:10 am
That would probably cause in lappending the same line several times into the list. Try this:
Code: Select all
foreach a [split [read [open $file1] \n] {
set f 0
foreach b [split [read [open $file2] \n] {
if {[string equal $a $b]} {
# Found it, so we set f to 1 and we break this 2nd loop
set f 1
break
}
}
# if f is still 0, then there's no match and we add $a to the list
if {!$f} { lappend c $a }
}
And don't forget to close the files after finishing from them.
romain
Voice
Posts: 12 Joined: Sun Oct 16, 2005 1:51 am
Post
by romain » Fri Dec 09, 2005 11:19 am
Ok thx i try.
it's not possible to use "lsearch" for verifier the data?
And i have
Code: Select all
file delete -force -- scripts/file1.txt
file rename -force -- scripts/file2.txt scripts/file1.txt
[17:10] Tcl error [pub:aj]: error deleting "scripts/file1.txt": permission denied
Sir_Fz
Revered One
Posts: 3794 Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:
Post
by Sir_Fz » Fri Dec 09, 2005 1:58 pm
Yes you can use lsearch to search through the list, as for deleting check your permissions.
romain
Voice
Posts: 12 Joined: Sun Oct 16, 2005 1:51 am
Post
by romain » Fri Dec 09, 2005 2:34 pm
Sir_Fz wrote: Yes you can use lsearch to search through the list, as for deleting check your permissions.
Sorry but how check permisions
For "lsearch" , Which is for you the fastest way ?
thx
Ofloo
Owner
Posts: 953 Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:
Post
by Ofloo » Fri Dec 09, 2005 4:01 pm
romain wrote: Sir_Fz wrote: Yes you can use lsearch to search through the list, as for deleting check your permissions.
Sorry but how check permisions
For "lsearch" , Which is for you the fastest way ?
thx
look at
file attributes &
file owned
tho i would just check there md5 against eachother.. if it doesn't match then i would go and compare.. and do all the rest .. ( if the files arn't big files.. ofcourse..)
XplaiN but think of me as stupid
Ehlanna
Voice
Posts: 15 Joined: Thu Jul 21, 2005 12:08 pm
Post
by Ehlanna » Sat Dec 10, 2005 5:37 am
Just a very silly thought on this ... how about using the command to shell out and run an OS sommand - is it exec? - and invoking the diff command? Why reinvent the wheel?
Sir_Fz
Revered One
Posts: 3794 Joined: Sun Apr 27, 2003 3:10 pm
Location: Lebanon
Contact:
Post
by Sir_Fz » Sat Dec 10, 2005 12:18 pm
Doing this through [file] is better for the performance and why use [exec] when you can do it through Tcl?
Ehlanna
Voice
Posts: 15 Joined: Thu Jul 21, 2005 12:08 pm
Post
by Ehlanna » Sat Dec 10, 2005 12:48 pm
Oh, I agree, writing it in tcl would be far more fun - just thought I would offer an 'obvious' alternative.