+ All Categories
Home > Documents > More Perl

More Perl

Date post: 07-Jan-2016
Category:
Upload: favian
View: 17 times
Download: 0 times
Share this document with a friend
Description:
More Perl. CIS*2450 Advanced Programming Concepts. \t \n \s \S \w \W \d \D. Tab character Newline character Matches any whitespace character Anything not \s [a-zA-Z0-9_] use \w+ to match a word Anything not \w [0-9] a digit Anything not \d [^0-9]. Shorthands in Perl. - PowerPoint PPT Presentation
31
1 More Perl CIS*2450 Advanced Programming Concepts
Transcript
Page 1: More  Perl

1

More Perl

CIS*2450

Advanced Programming Concepts

Page 2: More  Perl

2

Shorthands in Perl

\t

\n

\s

\S

\w

\W

\d

\D

• Tab character

• Newline character

• Matches any whitespace character

• Anything not \s

• [a-zA-Z0-9_] use \w+ to match a word

• Anything not \w

• [0-9] a digit

• Anything not \d [^0-9]

Page 3: More  Perl

3

Reading in a File

#!/usr/bin/perlwhile ( $_ = <ARGV> ) { print $_;}

#!/usr/bin/perlwhile ( <> ) { print;}

Page 4: More  Perl

4

Invert.pl

#!/usr/bin/perl # Usage: ./invert.pl filename$file = shift(@ARGV); # read in the 1st command line argument$ret = open(FH,$file); # open the file whose name was on the cmd line$i = 0;$lines[$i++] = tell(FH); # read in all lines and store their locationswhile ( <FH> ) { $lines[$i++] = tell(FH);}$i--;while ( $i >= 0 ) { # read in lines from the bottom to top seek(FH,$lines[$i--],0); $outp = <FH>; print $outp; # print out the line}

Page 5: More  Perl

5

Nonum.pl

#!/usr/bin/perl

while ( $_ = <ARGV> ) {

if ( /^[^0-9]+$/ ) {

print $_;

}

}

123

4

I see 5 dogs

I see five dogs

./nonum.pl testfile

????????????

Page 6: More  Perl

6

Nonum.pl

#!/usr/bin/perl

while ( $_ = <ARGV> ) {

if ( /^[^0-9]+$/ ) {

print $_;

}

}

123

4

I see 5 dogs

I see five dogs

./nonum.pl testfile

I see five dogs

Page 7: More  Perl

7

Nonum1.pl

#!/usr/bin/perl

while ( $_ = <ARGV> ) {

if ( /[^0-9]+/ ) {

print $_;

}

}

123

4

I see 5 dogs

I see five dogs

./nonum.pl testfile

??????????

Page 8: More  Perl

8

Nonum1.pl

#!/usr/bin/perl

while ( $_ = <ARGV> ) {

if ( /[^0-9]+/ ) {

print $_;

}

}

1234I see 5 dogsI see five dogs

./nonum.pl testfile1234I see 5 dogsI see five dogs

Page 9: More  Perl

9

Nonum2.pl

#!/usr/bin/perl

while ( $_ = <ARGV> ) {

$var = $_;

chop $var;

if ( $var =~ m/[^0-9]+/ ) {

print $_;

}

}

123

4

I see 5 dogs

I see five dogs

./nonum.pl testfile

??????????

Page 10: More  Perl

10

Nonum2.pl

#!/usr/bin/perl

while ( $_ = <ARGV> ) {

$var = $_;

chop $var;

if ( $var =~ m/[^0-9]+/ ) {

print $_;

}

}

123

4

I see 5 dogs

I see five dogs

./nonum.pl testfile

I see 5 dogs

I see five dogs

Page 11: More  Perl

11

Nonum.pl

#!/usr/bin/perl

while ( $_ = <ARGV> ) {

if ( /^\D+$/ ) {

print $_;

}

}

123

4

I see 5 dogs

I see five dogs

./nonum.pl testfile

I see five dogs

Page 12: More  Perl

12

Regexp quantfier#!/usr/bin/perlwhile ( $_ = <ARGV> ) { if ( /a{2,4}/ ) { # look for aa, aaa, aaaa $var1 = $'; # store text after match chop $var1; # remove newline print "(", $&, " ", $`, " ", $var1 , ")\n";# (text matched text before match text after match) } else { if ( /a+/ ) { # other amounts of a’s print "Any number of A's: ",$_; } }}

Page 13: More  Perl

13

cab

ccaabb

cccaaabbb

ccccaaaabbbb

cccccaaaaabbbbb

?????

Page 14: More  Perl

14

cab

ccaabb

cccaaabbb

ccccaaaabbbb

cccccaaaaabbbbb

Any number of A’s: cab

Page 15: More  Perl

15

cab

ccaabb

cccaaabbb

ccccaaaabbbb

cccccaaaaabbbbb

Any number of A’s: cab

(aa cc bb)

Page 16: More  Perl

16

cab

ccaabb

cccaaabbb

ccccaaaabbbb

cccccaaaaabbbbb

Any number of A’s: cab

(aa cc bb)

(aaa ccc bbb)

Page 17: More  Perl

17

cab

ccaabb

cccaaabbb

ccccaaaabbbb

cccccaaaaabbbbb

Any number of A’s: cab

(aa cc bb)

(aaa ccc bbb)

(aaaa cccc bbbb)

Page 18: More  Perl

18

cab

ccaabb

cccaaabbb

ccccaaaabbbb

cccccaaaaabbbbb

Any number of A’s: cab

(aa cc bb)

(aaa ccc bbb)

(aaaa cccc bbbb)

(aaaa ccccc abbbbb)

Page 19: More  Perl

19

After-Match Special Variables

$`

$&

$'

Text before match

Text matched

Text after match

Page 20: More  Perl

20

Capturing

• After a successful match, Perl provides variables $1, $2, $3 … which hold the text matched by their respective ( parenthesized subexpressions ) in the regex.– Subexpressions are numbered by counting open

parentheses from the left starting at 1

Page 21: More  Perl

21

#!/usr/bin/perl

while ( $_ = <ARGV> ) {

if ( /(c{1,3})(a{2,4})/ ) {

print $2, " ", $1, "\n";

}

}

Page 22: More  Perl

22

cab

ccaabb

cccaaabbb

ccccaaaabbbb

cccccaaaaabbbbb

?????

Page 23: More  Perl

23

cab

ccaabb

cccaaabbb

ccccaaaabbbb

cccccaaaaabbbbb

aa cc

aaa ccc

aaaa ccc

aaaa ccc

Page 24: More  Perl

24

Command Line Arguments

#!/usr/bin/perl# Extract the first argument from the command line# and place it into the variable $pattern. All # other arguments are treated as filenames.$pattern = shift(@ARGV);while ( <ARGV> ) { if ( /$pattern/ ) { print $ARGV,": ",$_; }}

Page 25: More  Perl

25

Looking for a String in a File

File1SetSize

ResetSize

SETSIZE

resetSIZE

File2This is a new file

and its Size is very small.

./pgm2.pl Size File*???????????

Page 26: More  Perl

26

Looking for a String in a File

File1SetSize

ResetSize

SETSIZE

resetSIZE

File2This is a new file

and its Size is very small.

./pgm2.pl Size File*File1: SetSize

File1: ResetSize

File2: and its Size is very small

Page 27: More  Perl

27

Looking for a String in a File

File1SetSize

ResetSize

SETSIZE

resetSIZE

File2This is a new file

and its Size is very small.

./pgm2.pl “is .* small” File*???????????

Page 28: More  Perl

28

Looking for a String in a File

File1SetSize

ResetSize

SETSIZE

resetSIZE

File2This is a new file

and its Size is very small.

./pgm2.pl “is .* small” File*File2: and its Size is very small.

Page 29: More  Perl

29

#!/usr/bin/perlprint "Do you want Greenwich Time (yes or no)?: ";# Get the input from STDIN$answer = <STDIN>;chop $answer;if ( $answer eq "yes" ) { $seconds = time; @datetime = gmtime($seconds); print "Time: ",$datetime[5]," ",$datetime[4]," ",$datetime[3]," ",$datetime[2]," ",$datetime[1],"

",$datetime[0],"\n";}else { $seconds = time; @datetime = localtime($seconds); print "Time: ",$datetime[5]," ",$datetime[4]," ",$datetime[3]," ",$datetime[2]," ",$datetime[1],"

",$datetime[0],"\n";}

Page 30: More  Perl

30

# Check to see if there are any command line arguments.

# The ARGV special variable contains all the command line

# arguments after the program name.

print "The number of command line arguments: ",$#ARGV+1,"\n";

# $#ARGV returns the index number of the last item on the

# command line, i.e. if the command line was:

# pgm.pl one two three

# then $#ARGV would be set to 2.

Page 31: More  Perl

31

$i = 0;if ( $#ARGV > -1 ) { while ( $i <= $#ARGV ) { if ( -e $ARGV[$i] ) { if ( -d $ARGV[$i] ) { print $ARGV[$i]," is a directory.\n"; } elsif ( -x $ARGV[$i] ) { print $ARGV[$i]," is an executable file.\n"; } elsif ( -T $ARGV[$i] ) { print $ARGV[$i]," is a text file.\n"; } } else { print $ARGV[$i]," is not a file.\n"; } $i++; }}


Recommended