Can anyone help with some Tcl?

Discussion in 'OT Technology' started by Penguin Man, Dec 29, 2003.

  1. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    So I'm writing this little program in Tcl to join files (you know, like binaries downloaded from newsgroups split into .001 .002 etc), as there is a lack of linux splitters/joiners that are any good. I have it working on the commandline and I'm trying to build a Tk interface (mostly for my own learning, but also because I might like to release it at some point). The problem I run into is with variables. When I run the joinFiles procedure by clicking the Join button, it tells me that the global variables don't exist, when clearly they do and I've defined them as global.

    So, here's my code. Anyone see the problem?

    Code:
    #!/usr/bin/tclsh
    
    package require Tk;
    
    #######################################################################
    # Adam Wolfe Gordon                                                   #
    # Joiner (maybe splitter at some point)                               #
    # 12/28/2003                                                          #
    # Purpose: To join split files                                        #
    #######################################################################
    
    # Interface ###########################################################
    # Set window information
    wm title . "Joiner";
    wm resizable . false false;
    wm geometry . 375x150;
    
    # Create a frame
    variable top;
    set top [frame .top -borderwidth 10 -width 375 -height 150];
    place $top -in . -relx 0 -rely 0;
    
    # Filename, number of parts, deletion option, skip option
    variable filename, parts, delete, skip;
    # And boxes/labels for them
    variable filenameBox, filenameLabel, partsBox, partsLabel, deleteBox, skipBox, action, quit;
    
    # Define the interface objects
    set filenameBox [entry .top.filenameBox -width 40 -textvariable filename -bg white];
    set filenameLabel [label .top.filenameLabel -text Filename:];
    
    set partsBox [entry .top.partsBox -width 4 -textvariable parts -bg white]
    set partsLabel [label .top.partsLabel -text {Number of last part:}];
    
    set deleteBox [checkbutton .top.deleteBox -text {Delete parts files} -variable delete];
    set skipBox [checkbutton .top.skipBox -text {Skip missing files} -variable skip];
    set action [button .top.action -text {Join!} -command joinFiles -default active];
    set quit [button .top.quit -text {Exit} -command exit];
    
    # Place the interface objects and set defaults
    place $filenameLabel -in $top -x 0 -y 0 -anchor nw;
    place $filenameBox -in $top -x 65 -y 0 -anchor nw;
    place $partsLabel -in $top -x 0 -y 25 -anchor nw;
    place $partsBox -in $top -x 130 -y 25 -anchor nw;
    place $deleteBox -in $top -x 0 -y 50 -anchor nw;
    place $skipBox -in $top -x 0 -y 75 -anchor nw;
    place $action -in $top -x 0 -y 100 -anchor nw;
    place $quit -in $top -x 350 -y 100 -anchor ne;
    
    # Guts ################################################################
    proc joinFiles {} {
    	# Previous Variables
    	global filename, parts, delete, skip;
    	
    	# For the joining loop
    	variable i, ext;
    	set i 1;
    	
    	while {$i <= $parts} {
    		# Change the extension
    		if {$i < 10} {
    			set ext "00$i";
    		} elseif {$i < 100} {
    			set ext "0$i";
    		} else {
    			set ext "$i";
    		}
    		
    		# Filenames with extension
    		variable filewext;
    		set filewext "$filename.$ext";
    		
    		# Check whether each file exists
    		if {[file exists $filewext]} {
    			# Add it using cat if it does
    			exec /bin/cat $filewext >> $filename;
    			puts "Adding part $i";
    			
    			# Delete parts if requested
    			if {$delete == "y"} {
    				file delete -force $filewext;
    			}
    		} else {
    			# Tell the user if the file doesn't exist ... Exit if they chose not to skip parts
    			if {$skip != "n"} {
    				puts "Missing part $i";
    			} else {
    				puts "Missing part $i ... Exiting!";
    				break;
    			}
    		}
    		
    		# Duh...  This is important
    		incr i;
    	}
    }
     
  2. ph1

    ph1 Guest

    lose the commas between the variable names in the global declaration.
     
  3. Penguin Man

    Penguin Man Protect Your Digital Liberties

    Joined:
    Apr 27, 2002
    Messages:
    21,696
    Likes Received:
    0
    Location:
    Edmonton, AB
    :bowdown: :bowdown: Works great now. I'll have to write that into my textbook so I remember next time :o

    I really didn't expect anyone on here to know Tcl but thought it was worth a shot.
     

Share This Page