Thread Vergleichsoperator in Variable
(9 answers)
Opened by tcicit at 2011-02-10 16:20
So kann man es machen:
Code (perl): (dl
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 my %cmds=( '<=' => sub{ $_[0] <= $_[1] }, '==' => sub{ $_[0] == $_[1] }, '>=' => sub{ $_[0] >= $_[1] }, 'or' => sub{ $_[0] or $_[1] }, 'and' => sub{ $_[0] and $_[1] }, ); my $cmd='<='; my $param1=1; my $param2=2; if(exists($cmds{$cmd})) { if($cmds{$cmd}->($param1,$param2)) { print "$param1 ist $cmd $param2\n"; } else { print "$param1 ist nicht $cmd $param2\n"; } } else { warn(qq(UNKOWN COMMAND "$cmd"!)); } EDIT: Tippfehler beseitigt. Danke für den Hinweis. Last edited: 2011-02-10 18:32:52 +0100 (CET) |