Schrift
[thread]5253[/thread]

textvariable: Verweis auf Arrayelement klemmt

Leser: 1


<< >> 5 Einträge, 1 Seite
FIFO
 2006-11-08 17:52
#45918 #45918
User since
2005-06-01
469 Artikel
BenutzerIn

user image
Warum klappt das nicht:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use warnings;
use strict;
use Tk;

my @data = qw(null eins zwei drei);
my @data_2 = qw(nil one two three);

my @display;

my $mw = MainWindow->new;

for my $i (1..3) {
push @display, $mw->Label(
-textvariable=>\$data[$i],
) ->pack;
}

my $toggle_button = $mw->Button(
-text=>"English!",
-command=>sub{ @data = @data_2},
)->pack;

MainLoop;


Eine Änderung im @data-Array sollte sich auf die Anzeige auswirken, oder mach ich da einen Denkfehler?

Gruß, FIFO
Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it? -- Brian Kernighan: "The Elements of Programming Style"
styx-cc
 2006-11-08 18:35
#45919 #45919
User since
2006-05-20
533 Artikel
BenutzerIn

user image
Ich hab dir das mal so umgeschrieben, dass es funktioniert, aber ganz genau erklaeren warum das nicht funktioniert kann ich dir auch nicht, scheinbar funktioniert es nur, wenn du explizit angibst welches Element von @data geaendert werden soll...

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/usr/bin/perl
use strict;
use warnings;

use Tk;

my @data = qw(null eins zwei drei);
my @data_2 = qw(nil one two three);

my @display;

my $mw = MainWindow->new;

for my $i ( 1..scalar(@data) ) {

push @display, $mw->Label(
-textvariable=>\$data[$i],
) ->pack;
}

my $toggle_button = $mw->Button(
-text=>"English!",
-command=>sub{ for (my $i =1; $i<scalar(@data); $i++) {$data[$i] = $data_2[$i]} },
)->pack;

MainLoop;


MfG
Pörl.
FIFO
 2006-11-08 20:16
#45920 #45920
User since
2005-06-01
469 Artikel
BenutzerIn

user image
Danke, diese Variante hatte ich noch nicht probiert! Das Warum würde ich trotzdem gern kapieren ;-)
Es macht übrigens keinen Unterschied, wenn die Array-Elemente keine konstanten Strings sind, sondern zur Laufzeit interpolierte Skalare.

Sei's drum, hast mir sehr geholfen!
Gruß FIFO
Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it? -- Brian Kernighan: "The Elements of Programming Style"
renee
 2006-11-08 20:45
#45921 #45921
User since
2003-08-04
14371 Artikel
ModeratorIn
[Homepage] [default_avatar]
Das erste funktioniert nicht, weil der Wert im Speicher nicht geändert wird, sondern die Elemente von @data verweisen einfach auf einen anderen Speicherbereich. Die Labels warten aber auf Änderungen im Speicher.

Vielleicht verdeutlicht folgendes das Phänomen etwas:
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use warnings;
use strict;
use Tk;

my @data = qw(null eins zwei drei);
my @data_2 = qw(nil one two three);

my @display;

my $mw = MainWindow->new;

for my $i (1..3) {
push @display, $mw->Label(
-textvariable=>\$data[$i],
) ->pack;
}

my $toggle_button = $mw->Button(
-text=>"English!",
-command=>sub{print \$data[1]; @data = @data_2; print \$data[1];},
)->pack;

MainLoop;


Ausgabe:
Code: (dl )
SCALAR(0x224fb8)SCALAR(0x2ba8064)



vs.
Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use warnings;
use strict;
use Tk;

my @data = qw(null eins zwei drei);
my @data_2 = qw(nil one two three);

my @display;

my $mw = MainWindow->new;

for my $i (1..3) {
push @display, $mw->Label(
-textvariable=>\$data[$i],
) ->pack;
}

my $toggle_button = $mw->Button(
-text=>"English!",
-command=>sub{print \$data[1]; for(0..scalar(@data) -1){$data[$_] = $data_2[$_]}; print \$data[1];},
)->pack;

MainLoop;


Ausgabe:
Code: (dl )
SCALAR(0x224fb8)SCALAR(0x224fb8)
OTRS-Erweiterungen (http://feature-addons.de/)
Frankfurt Perlmongers (http://frankfurt.pm/)
--

Unterlagen OTRS-Workshop 2012: http://otrs.perl-services.de/workshop.html
Perl-Entwicklung: http://perl-services.de/
FIFO
 2006-11-08 21:24
#45922 #45922
User since
2005-06-01
469 Artikel
BenutzerIn

user image
@renee: Danke, das war mir gar nicht so klar, es erklärt sogar vielleicht noch andere Merkwürdigkeiten in meinem Code, werd mal auf die Suche gehen ...

Gruß FIFO
Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it? -- Brian Kernighan: "The Elements of Programming Style"
<< >> 5 Einträge, 1 Seite



View all threads created 2006-11-08 17:52.