2016-06-13 16 views
0

Ich habe die meisten Probleme mit meinem Code gelöst, aber jetzt wird alles in meinem Listenfeld in eine Datei gedruckt, unabhängig davon, ob es aktiviert ist oder nicht. Nicht sicher warum. Jede Hilfe wird geschätzt. Ich hatte das vorher schon, aber ich bin mir nicht sicher, was ich getan habe, um es zu ändern. Ich möchte für, wenn ein Element aktiviert ist und Sie auf OK und dann auf Beenden klicken, dass nur die Elemente, die überprüft werden, in die Datei kopiert werden. Drucken in Datei mit Perl Tk

use strict; 
use warnings; 
use Tk; 
open(INFILE,"d1528235.txt") or die "could not open file for reading!\n"; 
open(OUTFILE, '>forecast.txt') or die "Cannot open forecast.txt: $!"; 


while(<INFILE>){ 
    if (m/^\s*$/) { next; } 
    chomp $_; 
    my @fields = split(/\ /,$_); 
    my @output; 
    foreach my $field(@fields){ 
    if($field =~ /^\*?[ABMQRWY][A-Z0-9]{4}235 /){ 
     push @output,$field; 

    } 


    } 

    if (@output) { 
    my $line = join('',@output); 
    print "$line\n"; 
    print OUTFILE "$line\n"; 
    } 
} 

my $mw = MainWindow->new; 
$mw->geometry("300x500"); 
$mw->title("Unforecasted Jobs"); 

my $check1 = 'NOT CHECKED'; 
my $check2 = 'NOT CHECKED'; 
my $check3 = 'NOT CHECKED'; 
my $check4 = 'NOT CHECKED'; 
my $check5 = 'NOT CHECKED'; 
my $check6 = 'NOT CHECKED'; 
my $check7 = 'NOT CHECKED'; 
my $check8 = 'NOT CHECKED'; 
my $check9 = 'NOT CHECKED'; 
my $check10 = 'NOT CHECKED'; 
my $check11 = 'NOT CHECKED'; 
my $check12 = 'NOT CHECKED'; 
my $check13 = 'NOT CHECKED'; 
my $check14 = 'NOT CHECKED'; 
my $check15 = 'NOT CHECKED'; 
my $check16 = 'NOT CHECKED'; 

my $check_frame = $mw->Frame()->pack(-side => "top"); 
$check_frame->Label(-text=>"Jobs That Do Not Forecast")->pack(-side => "top")->pack(); 
my $job = "18L"; 
my $chk1 = $check_frame->Checkbutton(-text => '18-Letters', 
           -variable => \$check1, 
           -onvalue => 'ADDED', 
           -offvalue => 'NOT CHECKED')->pack(), 
           -command => print OUTFILE "$job\n"; 

my $job2 = "07O"; 
my $chk2 = $check_frame->Checkbutton(-text => '07-Orders', 
           -variable => \$check2, 
           -onvalue => 'ADDED', 
           -offvalue => 'NOT CHECKED')->pack(), 
           -command => print OUTFILE "$job2\n"; 

my $job3 = "36L"; 
my $chk3 = $check_frame->Checkbutton(-text => '36-Letters', 
           -variable => \$check3, 
           -onvalue => 'ADDED', 
           -offvalue => 'NOT CHECKED')->pack(); 
           -command => print OUTFILE "$job3\n"; 

my $job4 = "38L"; 
my $chk4 = $check_frame->Checkbutton(-text => '38-Letters', 
           -variable => \$check4, 
           -onvalue => 'ADDED', 
           -offvalue => 'NOT CHECKED')->pack(), 
           -command => print OUTFILE "$job4\n"; 

my $job5 = "11O"; 
my $chk5 = $check_frame->Checkbutton(-text => '11-Orders', 
           -variable => \$check5, 
           -onvalue => 'ADDED', 
           -offvalue => 'NOT CHECKED')->pack(), 
           -command => print OUTFILE "$job5\n"; 

my $job6 = "99L"; 
my $chk6 = $check_frame->Checkbutton(-text => '99-Letters', 
           -variable => \$check6, 
           -onvalue => 'ADDED', 
           -offvalue => 'NOT CHECKED')->pack(); 
           -command => print OUTFILE "$job6\n"; 

my $job7 = "21L"; 
my $chk7 = $check_frame->Checkbutton(-text => '21-Letters', 
           -variable => \$check7, 
           -onvalue => 'ADDED', 
           -offvalue => 'NOT CHECKED')->pack(), 
           -command => print OUTFILE "$job7\n"; 

my $job8 = "23L"; 
my $chk8 = $check_frame->Checkbutton(-text => '23-Letters', 
           -variable => \$check8, 
           -onvalue => 'ADDED', 
           -offvalue => 'NOT CHECKED')->pack(), 
           -command => print OUTFILE "$job8\n"; 

my $job9 = "OOO"; 
my $chk9 = $check_frame->Checkbutton(-text => 'Return to LNI', 
           -variable => \$check9, 
           -onvalue => 'ADDED', 
           -offvalue => 'NOT CHECKED')->pack(); 
           -command => print OUTFILE "$job9\n"; 

my $job10 = "EOSSS235"; 
my $chk10 = $check_frame->Checkbutton(-text => 'EOSSS235', 
           -variable => \$check10, 
           -onvalue => 'ADDED', 
           -offvalue => 'NOT CHECKED')->pack(), 
           -command => print OUTFILE "$job10\n"; 

my $job11 = "M4020235 20"; 
my $chk11 = $check_frame->Checkbutton(-text => 'M4020235', 
           -variable => \$check11, 
           -onvalue => 'ADDED', 
           -offvalue => 'NOT CHECKED')->pack(), 
           -command => print OUTFILE "$job11\n"; 

my $job12 = "PRTAR235"; 
my $chk12 = $check_frame->Checkbutton(-text => 'PRTAR235', 
           -variable => \$check12, 
           -onvalue => 'ADDED', 
           -offvalue => 'NOT CHECKED')->pack(); 
           -command => print OUTFILE "$job12\n"; 

my $job13 = "WR115235"; 
my $job13b = "WR415235"; 
my $chk13 = $check_frame->Checkbutton(-text => 'WR115235', 
           -variable => \$check13, 
           -onvalue => 'ADDED', 
           -offvalue => 'NOT CHECKED')->pack(), 
           -command => print OUTFILE "$job13\n$job13b\n"; 

my $job14 = "X01RA235"; 
my $chk14 = $check_frame->Checkbutton(-text => 'X01RA235', 
           -variable => \$check14, 
           -onvalue => 'ADDED', 
           -offvalue => 'NOT CHECKED')->pack(), 
           -command => print OUTFILE "$job14\n"; 

my $job15 = "X1514235 15"; 
my $chk15 = $check_frame->Checkbutton(-text => 'X1514235', 
           -variable => \$check15, 
           -onvalue => 'ADDED', 
           -offvalue => 'NOT CHECKED')->pack(); 
           -command => print OUTFILE "$job15\n"; 

my $job16 = "X4020235"; 
my $chk16 = $check_frame->Checkbutton(-text => 'X4020235', 
           -variable => \$check16, 
           -onvalue => 'ADDED', 
           -offvalue => 'NOT CHECKED')->pack(); 
           -command => print OUTFILE "$job16\n"; 


my $button_frame = $mw->Frame()->pack(-side => "bottom"); 
my $ok_button = $button_frame->Button(-text => 'OK', 
            -command => \&check_sub)->pack(-side => "left"); 
my $exit_button = $button_frame->Button(-text => 'Exit', 
            -command => sub{$mw->destroy})->pack(-side => "right"); 

sub check_sub { 
    my $check_msg = "18-Letters: $check1\n07-Orders: $check2\n36-Letters: $check3\n38-Letters: $check4\n11-Orders: $check5\n99-Letters: $check6\n21-Letters: $check7\n23-Letters: $check8\nReturn L&I: $check9\nEOSSS235: $check10\nM4020235: $check11\nPRTAR235: $check12\nWR115235: $check13\nWR415235: $check13\nX01RA235: $check14\nX1514235: $check15\nX4020235: $check16"; 
    $mw->messageBox(-message => "Check Button Summary:\n$check_msg", -type => "ok"); 
} 

MainLoop; 

my $var =`crossref.pl`; 

Antwort

2
my $chk8 = $check_frame->Checkbutton(
    -text => '23-Letters', 
    -variable => \$check8, 
    -onvalue => 'ADDED', 
    -offvalue => 'NOT CHECKED')->pack(), 
    -command => print OUTFILE "$job8\n"; 

Der -command Teil dieses Aufrufs muss innerhalb der Klammern auf Checkbutton(…) sein. (Wie geschrieben, es ist eine völlig separate Aussage.)

Darüber hinaus müssen Sie den Befehl in einem sub { … } umgeben, um es als Coderef zu übergeben, anstatt es sofort auszuführen und das Ergebnis zu übergeben.

0

Das Hauptproblem scheint die falsche Platzierung der schließenden Klammer zu sein und die print-Anweisung in sub {} wie @duskwuff erläutert nicht zu umbrechen. Darüber hinaus könnten Sie Ihren Code durch not repeating yourself besser managbar machen und Arrays für die Checkbuttons und deren Namen verwenden. Zum Beispiel:

use strict; 
use warnings; 

use Tk; 

my $mw = MainWindow->new; 
$mw->geometry("300x500"); 
$mw->title("Unforecasted Jobs"); 

my $check_frame = $mw->Frame()->pack(-side => "top"); 
$check_frame->Label(-text => "Jobs That Do Not Forecast") 
    ->pack(-side => "top"); 

my @cb_info = (
    ['18L', '18-Letters'], 
    ['07O', '07-Orders'], 
    ['36L', '36-Letters'], 
    ['38L', '38-Letters'], 
    ['11O', '11-Orders'], 
    ['99L', '99-Letters'], 
    ['21L', '21-Letters'], 
    ['23L', '23-Letters'], 
    ['OOO', 'Return to LNI'], 
    ['EOSSS235', 'EOSSS235'], 
    ['M4020235 20', 'M4020235'], 
    ['PRTAR235', 'PRTAR235'], 
    ['WR115235', ['WR115235','WR415235']], 
    ['X01RA235', 'X01RA235'], 
    ['X1514235 15', 'X1514235'], 
    ['X4020235', 'X4020235'] 
); 

my @check_buttons; 
my @msg_info; 
for my $item (@cb_info) { 
    my ($job, $info) = @$item; 
    my $print_string = $info; 
    push @check_buttons, 'NOT CHECKED'; 
    my $cb = \$check_buttons[-1]; 
    my $text = $info; 
    if (ref $info eq "ARRAY") { 
     $print_string = $info->[0] . "\n" . $info->[1]; 
     push @msg_info, [$info->[0], $cb]; 
     push @msg_info, [$info->[1], $cb]; 
     $text = $info->[0]; 
    } 
    else { 
     push @msg_info, [$info, $cb]; 
    } 
    my $chk = $check_frame->Checkbutton(
     -text => $text, 
     -variable => $cb, 
     -onvalue => 'ADDED', 
     -offvalue => 'NOT CHECKED', 
     -command => sub { print STDOUT "$print_string\n"}, 
    )->pack; 
} 

my $button_frame = $mw->Frame()->pack(-side => "bottom"); 
my $ok_button = $button_frame->Button(
    -text => 'OK', 
    -command => \&check_sub 
)->pack(-side => "left"); 
my $exit_button = $button_frame->Button(
    -text => 'Exit', 
    -command => sub{$mw->destroy} 
)->pack(-side => "right"); 

sub check_sub { 
    my $check_msg; 
    my $newline = ''; 
    for my $item (@msg_info) { 
     $check_msg .= $newline . $item->[0] . ": " . ${ $item->[1] }; 
     $newline = "\n"; 
    } 
    $mw->messageBox(
     -message => "Check Button Summary:\n$check_msg", 
     -type => "ok" 
    ); 
} 

MainLoop;