1. Trang chủ
  2. » Công Nghệ Thông Tin

Perl basice tutorial ebook

28 252 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 28
Dung lượng 124,5 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

What is Perl? Optimized for String Manipulation and File I/O  Full support for Regular Expressions... String Operation Arithmeticle less than or equal to = cmp compare, return 1, 0, -1

Trang 1

Perl Basics

A Perl Tutorial

NLP Course - 2006

Trang 2

What is Perl?

 Optimized for String Manipulation and File I/O

 Full support for Regular Expressions

Trang 3

Running Perl Scripts

 Download ActivePerl from ActiveState

 Just run the script from a 'Command Prompt'

Trang 4

Basic Syntax

 Only single line comments

Trang 5

Scalars and Identifiers

 A variable name

 Case sensitive

 A single value (string or numerical)

 Accessed by prefixing an identifier with '$'

 Assignment with '='

$scalar = expression

Trang 6

 With ' (apostrophe)

 Everything is interpreted literally

 With " (double quotes)

 Variables get expanded

 With ` (backtick)

 The text is executed as a separate process, and the output of the command is returned as the value of the string

Check 01_printDate.pl

Trang 7

String Operation Arithmetic

le less than or equal to <=

ge greater than or equal to >=

cmp compare, return 1, 0, -1 <=>

Comparison Operators

Trang 8

Operator Operation

||, or logical or

&&, and logical and

!, not logical not

xor logical xor

Logical Operators

Trang 9

$newstring = $string1 $string2; #"potatohead"

$newerstring = $string1 x 2; #"potatopotato"

$string1 = $string2; #"potatohead"

String Operators

Check concat_input.pl

Trang 10

Perl Functions

 Perl functions are identified by their unique names

(print, chop, close, etc)

 Function arguments are supplied as a comma

separated list in parenthesis

 The commas are necessary

 The parentheses are often not

 Be careful! You can write some nasty and unreadable

code this way!

Check 02_unreadable.pl

Trang 11

 Ordered collection of scalars

 Zero indexed (first item in position '0')

 Elements addressed by their positions

Trang 13

 @: reference to the array (or a portion of it, with [])

 $: reference to an element (used with [])

Trang 15

Associative Arrays - Hashes

 Arrays indexed on arbitrary string values

 Key-Value pairs

 Use the "Key" to find the element that has the

"Value"

 % : refers to the hash

 {}: denotes the key

 $ : the value of the element indexed by the key

(used with {})

Trang 17

Arrays Example

#!/usr/bin/perl

# Simple List operations

# Address an element in the list

# Join elements at positions 0, 1, 2 and 4 into a

white-space delimited string

print("orchestral brass: ",

join(" ",@brass[0,1,2,4]),

"\n");

@unsorted_num = ('3','5','2','1','4');

@sorted_num = sort( @unsorted_num );

# Sort the list

@numbers_10, "\n");

# Remove the last print("Numbers (1-9): ", pop(@numbers_10), "\n");

# Remove the first print("Numbers (2-9): ", shift(@numbers_10), "\n");

# Combine two ops print("Count elements (2-9): ",

$#@numbers_10;

# scalar( @numbers_10 ), "\n");

print("What's left (numbers 2-9): ",

@numbers_10, "\n");

Trang 18

Hashes Example

#!/usr/bin/perl

# Simple List operations

$player{"clarinet"} = "Susan Bartlett";

$player{"basson"} = "Andrew Vandesteeg";

$player{"flute"} = "Heidi Lawson";

$player{"oboe"} = "Jeanine Hassel";

Trang 19

Pattern Matching

searched for in a character string

/pattern/

 =~: tests whether a pattern is matched

 !~: tests whether patterns is not matched

Trang 20

Pattern Matches Pattern Matches /def/ "define" /d.f/ dif

/\bdef\b/ a def word /d.+f/ dabcf

/^def/ def in start of

line

/d.*f/ df, daffff

/^def$/ def line /de{1,3}f/ deef, deeef

/de?f/ df, def /de{3}f/ deeef

/d[eE]f/ def, dEf /de{3,}f/ deeeeef

/d[^eE]f/ daf, dzf /de{0,3}f/ up to deeef

Patterns

Trang 21

Character Ranges

Escape

Sequence Pattern Description

\d [0-9] Any digit

\D [^0-9] Anything but a digit

\w [_0-9A-Za-z] Any word character

\W [^_0-9A-Za-z] Anything but a word char

\s [ \r\t\n\f] White-space

\S [^\r\t\n\f] Anything but white-space

Trang 23

Pattern Matching Options

Escape Sequence

Trang 25

Predefined Read-only Variables

EXAMPLE

$_ = "this is a sample string";

/sa.*le/; # matches "sample" within the string

Trang 26

The split and join Functions

The split function takes a regular expression and a string, and looks for all occurrences of the regular expression within that string The parts of the string that don't match the regular expression are returned in sequence as a list of values.

The join function takes a list of values and glues them together with a glue string between each list element.

Trang 27

String - Pattern Examples

Trang 28

String – Pattern Example

Ngày đăng: 23/10/2014, 16:11

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN