1 2 3 4
if ($string eq 'Trig' || $string eq 'Cos' || $string eq 'Sin') { # dann mach was }
1 2 3 4
use List::Util qw( any ); if ( any { $string eq $_ } qw( Trig Sin Cos ) ) { # ... }
if ($string ~~ ['Trig', 'Cos', 'Sin'])
":Sin:Cos:Trig:" =~ /:$string:/
2015-07-16T15:26:12 tibowskiHi, kann man in perl sowas realisieren?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#!/usr/bin/perl use strict; use warnings; sub isin { my $a = pop; for (@_) { if ($_ eq $a) { return 1; } } return 0; } my $string = "Trig"; if(isin(('Trig', 'Cos', 'Sin'), $string)) { print "Is in.\n"; }
1 2 3 4 5 6 7 8 9 10
use SetObject; my $string = 'Cos'; if ($string eq Set('Trig', 'Cos', 'Sin')) { say 'JA'; } else { say 'NEIN'; }
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 27 28 29 30
package SetObject; use overload 'eq' => \&str_compare, '==' => \&num_compare, fallback => 1; use base 'Exporter'; @EXPORT = 'Set'; sub Set { return bless [ @_ ], __PACKAGE__; } sub str_compare { my ($self, $other, $swap) = @_; for my $val (@$self) { return 1 if $val eq $other; } return 0; } sub num_compare { my ($self, $other, $swap) = @_; for my $val (@$self) { return 1 if $val == $other; } return 0; } 1;
edit: Variablennamen $value zu $string korrigiert