CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
If/else statements to create variables

 



Alpha1980
Novice

Jan 28, 2007, 2:29 PM

Post #1 of 4 (328 views)
If/else statements to create variables Can't Post

I have a Perl CGI script that takes variables and values from an HTML web form and processes them.

What I would like to do is create new variables and assign a value depending on whether certain variables exist in the web form and depending on the values.

I'm sure I can figure this out for myself with if/else statements but would appreciate being pointed in the right direction in terms of syntax.

I hope I've made myself clear.

Many Thanks.


KevinR
Veteran


Jan 28, 2007, 3:23 PM

Post #2 of 4 (327 views)
Re: [Alpha1980] If/else statements to create variables [In reply to] Can't Post

there's the ternary operator:


Code
my $value_from_form = 'foo'; 
my $other_variable = $value_from_form eq 'foo' ? 'foo' : 'bar';


or you can use the || operator to assign a value:


Code
my $value_from_form = param('name') || 'foo';


or use 'if/else', there is nothing wrong with that.
-------------------------------------------------


Alpha1980
Novice

Feb 5, 2007, 6:35 PM

Post #3 of 4 (319 views)
Re: [KevinR] If/else statements to create variables [In reply to] Can't Post

Thanks once again Kevin. :)

I've tried to write a script using your guidance but I ended up pulling my hair out. :(

I have written the logic below in plain English for what I want to do. Would this be easy to translate into perl?

I appreciate any advice.

Thanks.


Quote
if $critical has a value of emergency
create $charge and give it a value of 1

if $critical does not have a value
and
if $short has a value of yes
create $charge and give it a value of 2

if $critical does not have a value
and
if $short has a value of yes
and
(if $pshortflow has a value of 0.1, 0.2 or 0.3
and
if $pshorthours has a value of 1)
or
(if $sshortflow has a value of 1
and
if $sshorthours has a value of 1)
create $subcharge and give it a value of A

if $critical does not have a value
and
if $short has a value of yes
and
(if $pshortflow has a value of 0.4, 0.5 or 0.6
and
if $pshorthours has a value of 2, 3 or 4)
or
(if $sshortflow has a value of 1
and
if $sshorthours has a value of 2, 3 or 4)
create $subcharge and give it a value of B

if $critical does not have a value
and
if $long has a value of yes
and
if $amb does not have a value
create $charge and give it a value of 3

if $critical does not have a value
and
if $long has a value of yes
and
if $amb has a value of yes
and
if $amblw has a value of no
create $charge and give it a value of 4

if $critical does not have a value
and
if $long does not have a value
and
if $amb has a value of yes
and
if $amblw has a value of no
create $charge and give it a value of 5

if $critical does not have a value
and
if $amb has a value of yes
and
if $amblw has a value of no
and
(if $pambflow has a value of 0.1, 0.2 or 0.3
and
if $pambhours has a value of 0.5)
or
(if $sambflow has a value of 1 or 1.5
and
if $sambhours has a value of 0.5)
create $subcharge and give it a value of (a)

if $critical does not have a value
and
if $amb has a value of yes
and
if $amblw has a value of no
and
if $pambhours has a value of 1 or 1.5
or
if $sambflow has a value of 1 or 1.5
create $subcharge and give it a value of (b)

if $critical does not have a value
and
if $amb has a value of yes
and
if $amblw has a value of no
and
if $pambhours has a value of 1 or 1.5
or
if $sambflow has a value of 2 or 2.5
create $subcharge and give it a value of (c)

if $critical does not have a value
and
if $amb has a value of yes
and
if $amblw has a value of no
and
if $pambhours has a value of 1 or 1.5
or
if $sambflow has a value of 3 or 3.5
create $subcharge and give it a value of (d)

if $critical does not have a value
and
if $amb has a value of yes
and
if $amblw has a value of no
and
if $pambhours has a value of 1 or 1.5
or
if $sambflow has a value of 4, 4.5 or 5
create $subcharge and give it a value of (e)

if $critical does not have a value
and
if $amb has a value of yes
and
if $amblw has a value of no
and
if $pambhours has a value of 2, 2.5, 3 or 3.5
or
if $sambflow has a value of 0.5
create $subcharge and give it a value of (f)

if $critical does not have a value
and
if $amb has a value of yes
and
if $amblw has a value of no
and
if $pambhours has a value of 2, 2.5, 3 or 3.5
or
if $sambflow has a value of 1 or 1.5
create $subcharge and give it a value of (g)

if $critical does not have a value
and
if $amb has a value of yes
and
if $amblw has a value of no
and
if $pambhours has a value of 2, 2.5, 3 or 3.5
or
if $sambflow has a value of 2 or 2.5
create $subcharge and give it a value of (h)

if $critical does not have a value
and
if $amb has a value of yes
and
if $amblw has a value of no
and
if $pambhours has a value of 2, 2.5, 3 or 3.5
or
if $sambflow has a value of 3 or 3.5
create $subcharge and give it a value of (i)

if $critical does not have a value
and
if $amb has a value of yes
and
if $amblw has a value of no
and
if $pambhours has a value of 2, 2.5, 3 or 3.5
or
if $sambflow has a value of 4, 4.5 or 5
create $subcharge and give it a value of (j)

if $critical does not have a value
and
if $amb has a value of yes
and
if $amblw has a value of no
and
if $pambhours has a value of 4, 4.5, 5 or 5.5
or
if $sambflow has a value of 0.5
create $subcharge and give it a value of (k)

if $critical does not have a value
and
if $amb has a value of yes
and
if $amblw has a value of no
and
if $pambhours has a value of 4, 4.5, 5 or 5.5
or
if $sambflow has a value of 1 or 1.5
create $subcharge and give it a value of (l)

if $critical does not have a value
and
if $amb has a value of yes
and
if $amblw has a value of no
and
if $pambhours has a value of 4, 4.5, 5 or 5.5
or
if $sambflow has a value of 2 or 2.5
create $subcharge and give it a value of (m)

if $critical does not have a value
and
if $amb has a value of yes
and
if $amblw has a value of no
and
if $pambhours has a value of 4, 4.5, 5 or 5.5
or
if $sambflow has a value of 3 or 3.5
create $subcharge and give it a value of (n)

if $critical does not have a value
and
if $amb has a value of yes
and
if $amblw has a value of no
and
if $pambhours has a value of 4, 4.5, 5 or 5.5
or
if $sambflow has a value of 4, 4.5, 5 or 5.5
create $subcharge and give it a value of (o)

if $critical does not have a value
and
if $amb has a value of yes
and
if $amblw has a value of no
and
if $pambhours has a value of 6, 6.5, 7 or 7.5
or
if $sambflow has a value of 0.5
create $subcharge and give it a value of (p)

if $critical does not have a value
and
if $amb has a value of yes
and
if $amblw has a value of no
and
if $pambhours has a value of 6, 6.5, 7 or 7.5
or
if $sambflow has a value of 1 or 1.5
create $subcharge and give it a value of (q)

if $critical does not have a value
and
if $amb has a value of yes
and
if $amblw has a value of no
and
if $pambhours has a value of 6, 6.5, 7 or 7.5
or
if $sambflow has a value of 2 or 2.5
create $subcharge and give it a value of (r)

if $critical does not have a value
and
if $amb has a value of yes
and
if $amblw has a value of no
and
if $pambhours has a value of 6, 6.5, 7 or 7.5
or
if $sambflow has a value of 3 or 3.5
create $subcharge and give it a value of (s)

if $critical does not have a value
and
if $amb has a value of yes
and
if $amblw has a value of no
and
if $pambhours has a value of 6, 6.5, 7 or 7.5
or
if $sambflow has a value of 4, 4.5, 5 or 4.5
create $subcharge and give it a value of (t)

if $critical does not have a value
and
if $amb has a value of yes
and
if $amblw has a value of no
and
if $pambhours has a value of 8, 8.5, 9 or 9.5
or
if $sambflow has a value of 0.5
create $subcharge and give it a value of (u)

if $critical does not have a value
and
if $amb has a value of yes
and
if $amblw has a value of no
and
if $pambhours has a value of 8, 8.5, 9 or 9.5
or
if $sambflow has a value of 1 or 1.5
create $subcharge and give it a value of (v)

if $critical does not have a value
and
if $amb has a value of yes
and
if $amblw has a value of no
and
if $pambhours has a value of 8, 8.5, 9 or 9.5
or
if $sambflow has a value of 2 or 2.5
create $subcharge and give it a value of (w)

if $critical does not have a value
and
if $amb has a value of yes
and
if $amblw has a value of no
and
if $pambhours has a value of 8, 8.5, 9 or 9.5
or
if $sambflow has a value of 3 or 3.5
create $subcharge and give it a value of (x)

if $critical does not have a value
and
if $amb has a value of yes
and
if $amblw has a value of no
and
if $pambhours has a value of 8, 8.5, 9 or 9.5
or
if $sambflow has a value of 4, 4.5, 5 or 5.5
create $subcharge and give it a value of (y)

if $critical does not have a value
and
if $long has a value of yes
and
if $amb has a value of yes
and
if $amblw has a value of yes
create $charge and give it a value of 6

if $critical does not have a value
and
if $long does not have a value
and
if $amb has a value of yes
and
if $amblw has a value of yes
create $charge and give it a value of 7



KevinR
Veteran


Feb 5, 2007, 7:19 PM

Post #4 of 4 (318 views)
Re: [Alpha1980] If/else statements to create variables [In reply to] Can't Post

I'll (try) and do the first few lines and you can try and take it from there:


Code
if ($critical eq  'emergency') { 
$charge = 1;
}
elsif (!$critical && $short eq 'yes') {
$charge = 2;
}
elsif (!$critical && $short eq 'yes' &&
($pshortflow == 0.1 || $pshortflow == 0.2 || $pshortflow == 0.3) &&
($pshorthours == 1 || ($sshortflow == 1 && $sshorthours == 1) {
$subcharge = 'A';
}

-------------------------------------------------


(This post was edited by KevinR on Feb 5, 2007, 8:45 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives