• No results found

EGTDC Perl Course 2004

N/A
N/A
Protected

Academic year: 2021

Share "EGTDC Perl Course 2004"

Copied!
19
0
0

Loading.... (view fulltext now)

Full text

(1)

EGTDC Perl Course 2004

Dan Swan: Flow Control ([email protected])

(2)

Statement Blocks

A statement block is a way of executing statements, in sequence.

They look like:

{

first_statement;

second_statement;

third_statement;

}

Why do we have them?

Very useful for blocking off bits of code so that one is executed if one  condition is true but another is executed if it is not.

There are various ways of making decisions in Perl.

(3)

A note on equality

• When we use control structures, we generally compare one thing to  another.

• What we are looking for in generalised terms is "do X if A=B" or "do Y if  A=C".

• When comparing scalars you can compare them in a numerical context  or a string context.

• Equals:

$integer == 1

$string eq "perl"

• Not equals:

$integer != 1

(4)

IF/ELSE

• A control expression that IF the condition is true, one statement block is  executed, ELSE a different statement block is executed (ifelse.pl).

if (control_expression is TRUE) {

do this;

and this;

} else {

do that;

and that;

}

(5)

#ifelse.pl use warnings;

use strict;

if (1 == 2) {

print "1 is equal to 2!\n";

} else {

print "1 is not equal to 2, silly\n";}

exit();

(6)

ELSIF

• if/else is great for yes/no decisions.  If you want to test multiple  statements you can combine else and if to make 'elsif' (elsif.pl).

if (condition 1 is TRUE) {

do this;

} elsif (condition 2 is TRUE) {

do that;

} elsif (condition 3 is TRUE) {

do the other;

} else { #all tests are failed

do whatever;

}

(7)

#elsif.pl

use warnings;

use strict;

if (1 == 2) {

print "1 equals 2!\n";

} elsif (1 == 3) {

print "1 equals 3!\n";

} elsif (1 ==1) {

print "1 equals 1!\n";

} else {

print "Something has gone tragically wrong\n";

}

exit();

(8)

WHILE

• Lets say you want to do a series of actions whilst a certain condition is  true (while.pl):

while (expression is true) {

do this;

do that;

do the other; #until no longer true

}

(9)

#while.pl

use warnings;

use strict;

my $number;

$number = 1;

#as an aside the above two lines could be written

#"my $number = 1;"

while ($number == 1) {

print "Scalar \$number is 1\n";

$number++;

}

print "Scalar \$number is no longer 1 and as it is

$number the loop has terminated!\n";

(10)

FOREACH

• Takes a list of values and assigns them to a scalar variable, which  executes a block of code (foreach.pl).

foreach $element (@list) {

do this;

do that;

do the_other; #until no more $element's

}

(11)

#foreach.pl use warnings;

use strict;

my ($element, @list);

@list = ("one", "two", "three", "four",

"five", "six", "seven", "moo");

foreach $element (@list) { print "$element\n";

}

exit();

(12)

FOREACH(2)

• There's a few things missing from this code snippet compared to the  previous one (foreach2.pl).

• No specification of $element this time.  And yet it still works!  Why?

foreach (@list) {

do this;

do that;

do the_other;

}

(13)

#foreach2.pl use warnings;

use strict;

my (@list);

@list = ("one", "two", "three", "four",

"five", "six", "seven", "moo");

foreach (@list) { print;

print "\n";

}

exit();

(14)

FOREACH(3)

• There is an implied scalar on the previous slide ­ $_

• The $_ is a special scalar variable ­ almost like a scratchpad ­ its a  container for information  (foreach3.pl).  Notice it works both for the  foreach AND the print statement.

• Perl knows that if you use foreach (@list) that it is going to assign  each element to a scalar ­ so it will use $_ by default.  

foreach $_ (@list) {

do this;

do that;

do the_other; #until no more $_'s

}

(15)

#foreach3.pl use warnings;

use strict;

my (@list);

@list = ("one", "two", "three", "four",

"five", "six", "seven", "moo");

foreach $_ (@list) { print "$_\n";

}

exit();

(16)

FOR

• The statement people remember from BASIC (or C!) 

• An initial expression is set up ($init_val), a test expression is then set up  which is tested at each iteration ($init_val < 10).  Finally the initial 

expression is changed with each loop iteration ($init_val++).

for ($init_val = 0; $init_val < 10; $init_val++) {

print "$init_val\n";

}

(17)

#for.pl

use warnings;

use strict;

my ($init_val);

for ($init_val = 1; $init_val < 10; $init_val++) {

print "The scalar \$init_val has value = $init_val\n";

}

print "\nThe loop has ended!\n";

exit();

(18)

A few asides ­ other control  structures

unless/else ­ like if/else ­ but unless (false) rather than if (true).

do/while and do/until ­ "does" a statement block "while" a  condition is evaluated or "does" a statement block "until" expression  is evaluated.

last ­ allows you to get out of a loop early ­ e.g. instead of loop  finishing when loop conditions are met ­ a loop can end when 

conditions internal to the loop are met.  See also "next" "redo" and  read up on "labelled blocks" for more info.

(19)

Licence

• This work is licensed under the Creative Commons Attribution­

NonCommercial­ShareAlike License. To view a copy of this license, visit  http://creativecommons.org/licenses/by­nc­sa/1.0/ or send a letter to  Creative Commons, 559 Nathan Abbott Way, Stanford, California 

94305, USA.

References

Related documents

To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco,

To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco,

The Sustainable Development Goals — also known as the SDGs or the Global Goals —cover a wide range of sustainability issues; highlighting local, national and international

4. File Tab – Has basic functions like “Open new or existing word file”, “Save file”, “Print file” etc. Ribbon and Minimize Ribbon –For each Tab, there is a separate

To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco,

Đối lập với quan điểm chung về hoạt động kinh doanh là ý thức hệ kinh doanh truyền thống như chúng ta đã đề cập đến trước đó: tách kinh doanh ra khỏi khía

To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco,

To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco,