more perl data types scalar: it may be a number, a character string, or a reference to another data...

9
Perl Data Types ar: it may be a number, a character string, or a reference to another data t -the sigil $ is used to denote a scalar(or reference) : an ordered collection of scalars (a variable that holds a list is called a -the sigil @ is used to denote a list(or array) : a map from strings to scalars. The strings are called keys and the lars are called values. ‘key/value’ pairs. -the sigil % is used to denote a hash. Variables ables hold the datatypes described above. ables use special sigils to indicate what type they are.

Upload: frank-mathews

Post on 18-Jan-2016

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: More Perl Data Types Scalar: it may be a number, a character string, or a reference to another data type. -the sigil $ is used to denote a scalar(or reference)

More Perl Data Types

Scalar: it may be a number, a character string, or a reference to another data type.-the sigil $ is used to denote a scalar(or reference)

List: an ordered collection of scalars (a variable that holds a list is called an array).-the sigil @ is used to denote a list(or array)

Hash: a map from strings to scalars. The strings are called keys and the scalars are called values. ‘key/value’ pairs.

-the sigil % is used to denote a hash.

Perl Variables

Variables hold the datatypes described above.Variables use special sigils to indicate what type they are.

Page 2: More Perl Data Types Scalar: it may be a number, a character string, or a reference to another data type. -the sigil $ is used to denote a scalar(or reference)

Data Types and Variables: Lists**an array holds a list.

my @scores = (“32”, “45”, “16”, “5”);

print “The first score: $scores[0]\n”;print “The third score: $scores[2]\n”;

Page 3: More Perl Data Types Scalar: it may be a number, a character string, or a reference to another data type. -the sigil $ is used to denote a scalar(or reference)

Perl Data Types and Variables: Hash

my %fruit_prices = {apple => ‘1.00’,orange => ‘2.50’

}

print “An apple costs:” . $fruit_prices{‘apple’} . “\n”;

Page 4: More Perl Data Types Scalar: it may be a number, a character string, or a reference to another data type. -the sigil $ is used to denote a scalar(or reference)

List(Array) & Hash Functions:

Hashes and Arrays have several native functions which work with them.

For a complete listing of these functions, see: http://perldoc.perl.org/index-functions-by-cat.html

Page 5: More Perl Data Types Scalar: it may be a number, a character string, or a reference to another data type. -the sigil $ is used to denote a scalar(or reference)

Variable Scope:

By declaring your variables with ‘my’, you are making that variable accesible to the current block of code you are in (defined by {…}), AND to any blocks containedwithin your current block.

Examples:

SEE: scope.pl AND scope2.pl

Page 6: More Perl Data Types Scalar: it may be a number, a character string, or a reference to another data type. -the sigil $ is used to denote a scalar(or reference)

More On Operators

http://www.pageresource.com/cgirec/ptut6.htmhttp://www.pageresource.com/cgirec/ptut7.htm

+ addition

- subtraction

* multiplication

/ division

% modulus

** exponent

= assignment

+= add and assign

-= subtract and assign

*= mult and assign

/= divide and assign

%= Modulus assign

**= Exponent assign

++ Increment (add1)

-- Decrement (subtract1)

. Concatenate strings

.= Concatenate and assign

== Equal to

!= Not equal to

> Greater than

< Less than

<= Less than or equal to

>= Greater than or equal to

eq Equal to

ne Not equal to

gt Greater than

lt Less than

ge Greater than or equal to

le Less than or equal to

&& and

|| or

! not

Arithmetic Operators

Arithmetic Assignment

String Concatenation

Numeric Comparison

String Comparison

Logical Operators

Page 7: More Perl Data Types Scalar: it may be a number, a character string, or a reference to another data type. -the sigil $ is used to denote a scalar(or reference)

Control Structures:

if/else- This takes a control expression and a block of code. The control expression is evaluated for truth. If the expression returns ‘true’ the block of code is executed. If it fails, the optional ‘else’ code block is executed, or the program continues to run skipping the ‘if’ code block. see: if_else.pl for examples.

while- While loops test one expression for truth, and will keep running the loop as long as the expression returns true. While loops are good for iterating over arrays or through lines in a file. see while.pl for examples.

for- for loops are also iterators which has a bit more complex expression testing syntax. see: for.pl for examples.

foreach- foreach loops iterate over the items in a list assigning the current value to a scalar for use in the loop. This is a great construct to use in processing arrays or lists. see: foreach.pl for examples.

Page 8: More Perl Data Types Scalar: it may be a number, a character string, or a reference to another data type. -the sigil $ is used to denote a scalar(or reference)

File Handles:

When we read or write files in perl, we use FILEHANDLES to access these files.The FILEHANDLE acts as a kind of variable we can use to read from a file, or write to a file.

The function we use to access a file is called open().

Examples: read_file.plwrite_file.plappend_file.pl

Page 9: More Perl Data Types Scalar: it may be a number, a character string, or a reference to another data type. -the sigil $ is used to denote a scalar(or reference)

Exercises