Ransom has asked for the wisdom of the Perl Monks concerning the following question:
Hey Monks,
I've got some code smell and I can't find the right cleaner. I'm looking to combine the following two functions. I ended up writing them real quick to get another module finished, but I don't like the repitition. I've got a feeling that I'm not really being pragmatic or not using the right tools. Without further ado:
# Simple num item in list check sub is_in_num { my ($item, $l1) = @_; foreach my $e1 (@$l1) { if ($e1 == $item) { return 1; } } return 0; } # Simple str item in list check sub is_in_str { my ($item, $l1) = @_; foreach my $e1 (@$l1) { if ($e1 eq $item) { return 1; } } return 0; }
Did a bit of searching and didn't find any elegant solutions, but this is a pretty simple problem so maybe it doesn't demand such attention
Ransom
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Better way to write these checks?
by moritz (Cardinal) on Jun 29, 2012 at 12:38 UTC | |
by Ransom (Beadle) on Jun 29, 2012 at 12:44 UTC | |
by Anonymous Monk on Jun 29, 2012 at 12:50 UTC | |
|
Re: Better way to write these checks?
by Anonymous Monk on Jun 29, 2012 at 12:42 UTC | |
by Ransom (Beadle) on Jun 29, 2012 at 12:45 UTC | |
|
Re: Better way to write these checks?
by Athanasius (Archbishop) on Jun 29, 2012 at 12:52 UTC | |
|
Re: Better way to write these checks?
by ww (Archbishop) on Jun 29, 2012 at 15:44 UTC | |
|
Re: Better way to write these checks?
by uday_sagar (Scribe) on Jun 29, 2012 at 13:27 UTC | |
by Anonymous Monk on Jun 29, 2012 at 13:48 UTC | |
by muba (Priest) on Jun 29, 2012 at 15:52 UTC | |
|
Re: Better way to write these checks?
by linuxkid (Sexton) on Jun 29, 2012 at 17:48 UTC |