Home > Archive > PERL Beginners > February 2006 > replacing multiple elsif statements question
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
replacing multiple elsif statements question
|
|
| Graeme McLaren 2006-02-27, 6:58 pm |
| Afternoon all, I want to change some repetitive code so its more dynamic,
basiaclly I want to replace the elsif statements below with one statement
which gets the number dynamically so I only need one if statement.
Any ideas?
Something like this:
elsif ($q->param('delete_aoc1.x')){
return 'Delete AOC 1';
} elsif ($q->param('delete_aoc2.x')){
return 'Delete AOC 2';
} elsif ($q->param('delete_aoc3.x')){
return 'Delete AOC 3';
} elsif ($q->param('delete_aoc4.x')){
return 'Delete AOC 4';
} elsif ($q->param('delete_aoc5.x')){
return 'Delete AOC 5';
}
To this:
elsif ($q->param("delete_aoc$number.x")){
return "Delete AOC $number";
}
Cheers in advance for any suggestions.
Cheers,
G :)
| |
| Tom Phoenix 2006-02-27, 6:58 pm |
| On 2/27/06, Graeme McLaren <iamnotregistered@hotmail.com> wrote:
> elsif ($q->param("delete_aoc$number.x")){
> return "Delete AOC $number";
You could do this sort of thing in a loop, such as a 'foreach' loop.
Have you tried writing it that way? Of course, you still have to call
the 'param' method once each time through the loop, until it returns a
true value. Hope this helps!
--Tom Phoenix
Stonehenge Perl Training
| |
| Chas Owens 2006-02-27, 6:58 pm |
| On 2/27/06, Graeme McLaren <iamnotregistered@hotmail.com> wrote:
> Afternoon all, I want to change some repetitive code so its more dynamic,
> basiaclly I want to replace the elsif statements below with one statement
> which gets the number dynamically so I only need one if statement.
>
> Any ideas?
snip
A lot depends on what $q is. There may be a better way to do what you
are trying, but given the information you have provided, this is the
best I can do
for my $num (1 .. 5) {
return "Delete AOC $num" if $q->param("delete_aoc$num.x");
}
| |
| Jeff Pang 2006-02-27, 9:56 pm |
|
>elsif ($q->param("delete_aoc$number.x")){
> return "Delete AOC $number";
>
> }
I'm not so sure,is it right for you?
elsif ($q->param(eval "delete_aoc$number.x")){
return "Delete AOC $number";
}
I just tested under my environment,it could work.
--
Jeff Pang
NetEase AntiSpam Team
http://corp.netease.com
| |
| John W. Krahn 2006-02-28, 3:56 am |
| Jeff Pang wrote:
>
>
>
> I'm not so sure,is it right for you?
>
> elsif ($q->param(eval "delete_aoc$number.x")){
eval() runs _Perl_ code. "delete_aoc$number.x" is NOT Perl code so you
shouldn't use eval() on it.
> return "Delete AOC $number";
>
> }
>
> I just tested under my environment,it could work.
John
--
use Perl;
program
fulfillment
|
|
|
|
|