Re: [TCL] diff type operations in TCL
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.tcl archive

Re: [TCL] diff type operations in TCL

From: Bruce Hartweg <bruce-news@hartweg.us>
Date: Fri Apr 28 2006 - 01:11:48 CEST

MH wrote:
> In article <s094g.55732$IZ2.1202@dukeread07>,
> Gerald W. Lester <Gerald.Lester@cox.net> wrote:
>> Andrew Falanga wrote:
>>> I didn't see anything in tcllib and thus far haven't turned up anything
>>> that looks like it will work. I would like to know if there is a TCL
>>> package, or already written procedure, that will do a simple diff of two
>>> files and tell me if they're the same or if they have differences.
>>>
>>> Is there anything like this?
>> If all you want to know is if they are different, then:
>>
>> set fd [open $file1 r]
>> set f1 [read $fd]
>> close $fd
>> set fd [open $file2 r]
>> set f2 [read $fd]
>> close $fd
>> if {[string equal $f1 $f2]} then {
>> puts "'$file1' and '$file2' are the same."
>> } else {
>> puts "'$file1' and '$file2' are different."
>> }
>
> But, if they're huge files, this can take a LOT of memory.. I prefer the
> unix "sum" for a quick diff.
>
> If the results are the same, the files are identical. If not, they are
> different.
>
> MH

if your using a unix command cmp would be better
(i.e. if it finds a difference early it stops reading the file,
sum needs to read the entire file, and there is a , albeit slight,
chance of 2 differing files having the same sum)

but either way you're limiting yourself platform wise. the above
code can be tweaked to compare chunks at a time and short circuit
the fail case as soona s a diff is found.

Bruce

Bruce
Received on Sun Apr 30 03:29:47 2006