Lösungshilfen für (Schul|Studien)Aufgaben

Inhalt:

Im Forum tauchen immer wieder die selben Fragen auf wenn es um Schul- oder Studienaufgaben geht. Vielleicht findest Du hier eine passende Anregung für eine Lösung...

Datei einlesen und mit Zeilennummer versehen

#!/usr/bin/perl

use strict;
use warnings;
use Tk;

my %variable_hash;
my $mw = MainWindow->new();
_build_gui($mw,%variable_hash);
MainLoop;

#------------------------------------------------------------------------------#
#                                Subroutines                                   #
#------------------------------------------------------------------------------#

sub _build_gui{
    my ($mw,%hash) = @_;
    
    my $input_frame  = $mw->Frame()->pack();
    my $output_frame = $mw->Frame()->pack();
    my $editor_frame = $mw->Frame()->pack();
    my $button_frame = $mw->Frame()->pack();
    
    my $input_label  = $input_frame->Label(
                                -text => 'Input',
                                          )->pack(-side => 'left');
    my $input_entry  = $input_frame->Entry(
                                -textvariable => \$hash{input},
                                          )->pack(-side => 'left');
    my $input_button = $input_frame->Button(
                                -text    => 'Durchsuchen...',
                                -command => [\&_get_input,$mw,\$hash{input}],
                                           )->pack(-side => 'left');
                                           
    my $output_label = $output_frame->Label(
                                -text => 'Output',
                                          )->pack(-side => 'left');
    my $output_entry = $output_frame->Entry(
                                -textvariable => \$hash{output},
                                           )->pack(-side => 'left');
    my $output_button= $output_frame->Button(
                                -text    => 'Durchsuchen...',
                                -command => [\&_get_output,$mw,\$hash{output}],
                                           )->pack(-side => 'left');
    
    my $message_lbl  = $editor_frame->Label(
                                -textvariable => \$hash{message},
                                           )->pack();
    my $listbox      = $editor_frame->Listbox(
                                -width    => 80,
                                -height   => 20,
                                             )->pack();
    
    my $start_button = $button_frame->Button(
                                -text    => 'start...',
                                -command => [\&_read_file,$listbox,\%hash])->pack();
}

sub _read_file{
    my ($list,$hashref) = @_;
    
    if(defined $hashref->{input} && defined $hashref->{output}){
        if(open(my $fh,'<',$hashref->{input}) && 
           open(my $w_fh,'>',$hashref->{output})){
            while(my $line = <$fh>){
                print $w_fh $. . " " . $line;
                chomp $line;
                $list->insert('end', $. . " " . $line);
            }
            close $fh;
            close $w_fh;
            $hashref->{message} = 'Aktion erfolgreich';
        }
        else{
            $hashref->{message} = 'Aktion nicht erfolgreich';
        }
    }
    else{
        $hashref->{message} = 'Bitte Eingabe- und Ausgabedatei festlegen';
    }
}

sub _get_input{
    my ($mw,$var_ref) = @_;    
    my $filetypes = [['Text files','.txt'],['Text files','.TXT']];
    $$var_ref = $mw->getOpenFile(-filetypes => $filetypes);
}

sub _get_output{
    my ($mw,$var_ref) = @_;
    my $filetypes = [['Text files','.txt'],['Text files','.TXT']];
    $$var_ref = $mw->getSaveFile(-filetypes => $filetypes);
}

Teil 2 ...

Ergänzungen, Kommentare

Kommentare werden am besten in folgender Form vorgenommen, damit sie im Inhaltsverzeichnis angezeigt werden (natürlich ohne das <verbatim>):
---+++ Main.??? - 14 Jul 2003 - Betreff

UtilFaqSubForm edit

Titel Lösungshilfen für (Schul|Studien)Aufgaben
Autor ReneeBaecker
Bereich FaqTK
Tags
Topic revision: 2008-12-07, StrUppi
 
Bitte die NutzungsBedingungen beachten.
Bei Vorschlägen, Anfragen oder Problemen mit dem PerlCommunityWiki bitten wir um WebBottomBarExample">Rückmeldung.