LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Tcl help anyone? (https://www.linuxquestions.org/questions/programming-9/tcl-help-anyone-129678/)

CanadianPenguin 12-29-2003 12:52 AM

Tcl help anyone?
 
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;
        }
}

TIA all!

LinuxLala 12-29-2003 03:48 AM

______________________________________________________________________________
# Filename, number of parts, deletion option, skip option
variable filename, parts, delete, skip;
______________________________________________________________________________

I guess that u missed out a global decleration here.

CanadianPenguin 12-29-2003 01:55 PM

Nope, that shouldn't be a global declaration, in Tcl global declarations go insice a procedure in order to adopt a variable defined in global scope.

Anyway, I figured out the problem thanks to someone on another board. Shouldn't have the commas between the variable names in the global declaration. It works perfectly now :D

LinuxLala 12-30-2003 02:00 AM

OK. Thanks for sharing that :)

CanadianPenguin 12-30-2003 05:42 PM

NP, thought I should post the solution so that this thread can help others in the future.


All times are GMT -5. The time now is 02:56 AM.