VARIABLES and CONTROL STRUCTURES

Anuncio
Elements of Programming in Perl
<H16-2/3>
VARIABLES and
CONTROL STRUCTURES
Josep F. Abril
[email protected]
MSc BioInformatics
Josep F. Abril – Elements of Programming in Perl – 2004/11/04
HG16.2 1/14
Basic Data Structures in Perl
$calar values
$
$dnaseq = "ATGGGTA";
@
$nucleotides[1]
@nucleotides[1,2]
@rrays of scalars
$#nucleotides
@nucleotides = ( "A", "C", "T", "G" );
0
\
1
ash variables
%
2
3
$stop_codons{ochre}
%stop_codons = ( "ochre" => "UAA",
"amber" => "UAG",
"opal" => "UGA");
References
$$sequence
$sequence = \$dnaseq;
MSc BioInformatics
Josep F. Abril – Elements of Programming in Perl – 2004/11/04
HG16.2 2/14
Code Blocks
lue
R-va
ator
oper
lue
L-va
(optional)
lue
L-va
block
label
$len = $end + $ori ; }
ator
oper
LBL: {
expression
(R-value)
expression
statement
block
MSc BioInformatics
Josep F. Abril – Elements of Programming in Perl – 2004/11/04
HG16.2 3/14
1
Control Structures
STATEMENT
MODIFIERS
STATEMENT
STATEMENT
STATEMENT
STATEMENT
STATEMENT
if EXPR;
unless EXPR;
while EXPR;
until EXPR;
foreach LIST;
do BLOCK while EXPR
do BLOCK until EXPR
if (EXPR) BLOCK
if (EXPR) BLOCK else BLOCK
if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK
COMPOUND
STATEMENTS
LABEL while (EXPR) BLOCK
LABEL while (EXPR) BLOCK continue BLOCK
LABEL for (EXPR; EXPR; EXPR) BLOCK
LABEL foreach VAR (LIST) BLOCK
LABEL foreach VAR (LIST) BLOCK continue BLOCK
LABEL BLOCK continue BLOCK
MSc BioInformatics
Josep F. Abril – Elements of Programming in Perl – 2004/11/04
HG16.2 4/14
True/False in Boolean Context
FALSE values:
The integer value
0
The floating point value
0.0
The string
'0'
The empty (null) string
''
The empty list
()
The special undefined value
undef
in numeric context
undef == 0
in string context
undef eq ''
All other values are TRUE
MSc BioInformatics
Josep F. Abril – Elements of Programming in Perl – 2004/11/04
HG16.2 5/14
Control Structures: if
if (condition) {
statements;
};
statement;
if
condition
TRUE
FALSE
statement;
(condition) && do {
statements;
};
statement;
MSc BioInformatics
Josep F. Abril – Elements of Programming in Perl – 2004/11/04
HG16.2 6/14
2
Control Structures: unless
unless (condition) {
statements;
};
statement;
unless
condition
(condition) || do {
statements;
};
FALSE
TRUE
statement;
statement;
MSc BioInformatics
Josep F. Abril – Elements of Programming in Perl – 2004/11/04
HG16.2 7/14
Control Structures: if/else
statement;
if
condition
FALSE
statement;
TRUE
if (condition) {
statements;
} else {
statements;
};
statement;
(condition) ? yes : no
statement;
MSc BioInformatics
Josep F. Abril – Elements of Programming in Perl – 2004/11/04
HG16.2 8/14
Control Structures: if/elsif/else
statement;
if
cond 1
TRUE
FALSE
elsif
cond 2
FALSE
else
BLOCK
BLOCK 1
TRUE
BLOCK 2
if (condition 1) {
statements;
} elsif (condition 2) {
statements;
} elsif (condition 3) {
statements;
...
} elsif (condition N) {
statements;
} else {
statements;
};
statement;
MSc BioInformatics
Josep F. Abril – Elements of Programming in Perl – 2004/11/04
HG16.2 9/14
3
Control Structures: loops
while (condition) {
statement;
};
statements;
until (condition) {
FALSE
loop
condition
};
statements;
for (initialization;
condition;
iteration) {
TRUE
LOOP
BLOCK
};
statements;
foreach (list) {
statement;
MSc BioInformatics
};
statements;
Josep F. Abril – Elements of Programming in Perl – 2004/11/04
HG16.2 10/14
Control Structures: do loops
do {
statement;
statements;
} while (condition);
LOOP
BLOCK
do {
FALSE
loop
condition
TRUE
statements;
} until (condition);
statement;
MSc BioInformatics
Josep F. Abril – Elements of Programming in Perl – 2004/11/04
Control Structures: continue
loop
condition
TRUE
LOOP
BLOCK
statement;
MSc BioInformatics
block
while (condition) {
statements;
} continue {
statements;
};
statement;
FALSE
HG16.2 11/14
CONTINUE
BLOCK
foreach (list) {
statements;
} continue {
statements;
};
Josep F. Abril – Elements of Programming in Perl – 2004/11/04
HG16.2 12/14
4
Loop Control
# ... Previous code
$counter = 0;
while ($counter < 100) {
# ... First statement set
($counter == 50) && ($counter++, redo);
# ... Second statement set
($counter == 25) && do {
#... more statements;
next;
};
# ... Third statement set
($counter == 99) && last;
# ... Fourth statement set
$counter = $counter + 1;
} continue {
# ... Continue statement set
print STDOUT $counter, "\n";
};
# ... More code
MSc BioInformatics
Josep F. Abril – Elements of Programming in Perl – 2004/11/04
HG16.2 13/14
Control Structures: switch
SWITCH: {
(condition1) && do {
statements; last SWITCH;
};
(condition2) && do {
statements; last SWITCH;
};
...
(conditionN-1) && do {
statements; last SWITCH;
};
statements; # conditionN (default)
};
MSc BioInformatics
Josep F. Abril – Elements of Programming in Perl – 2004/11/04
HG16.2 14/14
5
Descargar