分析案例教案how to use perl_第1页
分析案例教案how to use perl_第2页
分析案例教案how to use perl_第3页
分析案例教案how to use perl_第4页
分析案例教案how to use perl_第5页
已阅读5页,还剩46页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

A

Brief

Introduction

for

PerlZhengyong工欲善其事必先利其器Brief

Concept

of

PerlA

multipurpose

Language

Be

competent

for

almost

all

kinds

of

jobs,include

WEB

applicationsA

“Glue”

Language

Can

integrate

different

programs

into

a

wholeto

plish

whatever

work

you

want

A

Language

which

can

be

run

under

differentOS

systemsCan

be

run

under

more

than

20

OS

systemsInstallation

of

PerlLinux

or

UNIX

Need

not

install,

most

of

them

have

installeditWindowsDownload

install

file

fromTestUnder

command

line,

just

type

perl

–vThe

Latest

VersionPerl5.0XXXmay

be

the

latest

versionPerl

manualLinux

or

UNIXMan

perlPerldoc

perlPerldoc

blocknameWindowsPerldoc

perlPerldoc

blockNumber

Variable

and

stringNumberInteger

2Float

1.5Exponential

number

1.3E6Large

number,

use

“_”

substitute

“,”

123_34_56VariableScalar

$nameArray

@nameHash

array

%nameString“21321421

53b

3534”Always

use

backslash

to

shield

the

mean

for

some

specialcharacters,

such

as

/

and

or

give

some

special

meansSome

special

string

squence\n a

new

line\r

Enter\t

Tab\b

Backspace\u

upper

case

for

the

next

character\l

lower

case

for

the

next

character\\

“\”\’

“’”\”

“””Scalar

VariableExpression

of

a

scalar$scalar_nameTwo

kinds

of

valuesNumberInteger

(remember:

do

not

add

0

at

the

beginning

of

a

number,otherwise

the

complier

will

look

it

as

an

octal

number)FloatCharacter

and

StringUnlimited

length

for

a

string

in

PerlAdd

a

backslash

“\”

to

show

the

mean

of

“,

otherwise

it

will

be

locked

asa

sign

of

a

stringqq

and

q

function

can

be

used

to

substitute

and

to

represent

a

stringSpecial

variables$_,

$”,

$/,

$2

and

$$

are

belong

to

special

variablesList

&

ArrayExpression

for

an

array@array_name$array_name[index]Hash

Array%hash_name$hash_name{key}KeysvaluesPointer

and

Structure■■■■■Just

as

pointer

in

C

or

C++ExpressionCite

scalar$pointer_name=\$variablePrint

“$$pointer\n”Each

$

represents

an

pointerCite

array$pointer_name=\@variableprint

“$$pointer_name[index]\n”Cite

Hash$pointer_name=\%variableprint

“$$pointer_name{key}\n”Cite

parameter

for

functionLoop

and

Logical

controlLoopFor($i=a;$i<b,$i++){ block;}While(logical

control){Block;}Foreach

[index

or

keys]

(@a

or

%a){Block;}last:

jump

out

this

loopLogical

controlLogical

operatoreq,

>,

<,

<=,

>=,

!=||,

or,

and,

&&,

!Logical

controlIfIf(logical

block){block;}Regular

RuleImportanceOne

of

the

most

important

part

of

Perl

Can

operate

all

kinds

of

text

and

pick

out

neededinformation

Similar

to

regulars

in

UNIX

,

GNU

scripts

and

some

editors,

such

as

vi,

which

make

it

popular

in

many

types

of

jobsFind

more

information

<<Mastering

Regular

Expressions>>,

edited

byJeffery

E.F.

Friedl,

1997

or<PERL24小时精通>A

simple

regularm/simon/Here

simon

is

the

regular

rule,

which

is

usedto

match

the

target

object,

here

thetarget

object

is

$_m//

can

be

substituted

by

any characters,

such

as

m

:

:,

m,,Some

protected

characters:1.^

$

(

)

\

|

@

[

{

?

.

+

*Match

rule

of

regularLeft

to

rightReturn

value

equals

“true”

only

when

the whole

regular

expression

can

be

matchedThe

string

which

matches

the

regular expression

first

should

be

found

firstAfter

matched

the

first

one,

the

regular

will find

the

second,

third

one

until

all

of

the strings

are

matched,

for

this

reason,

the regular

rule

is

“greedy”!Elementary

characters■“.”

Dot

“.”

is

used

to

match

any

single

characterexcept

\t.i.e.

/p.t/

can

match

pot,

pat,

pit,

carpet,

pup_tent.

It

onlymatch

one

character,

more

or

less

than

one

characterare

all

wrong.special

charcters\n a

new

line\r

Enter\t

Tab\f a

new

pageElementary

charactersWildcard1.

“+”“+”

can

be

used

to

match

a

character

ahead

with

the

target

strings

atleast

one

timeI.e.:

/do+g/

can

match

houndog,

hotdog,

doooog,

dogiee,

but

not

matchDog,

dg2.

“*”like

“+”,

but

the

match

times

can

be

zero,

/do*g/

can

match

dg3.

“?”Match

no

more

than

one

time,

/do?g/

can

match

dg

or

dog,

but

not

doog4.

Pat{n,m}Match

any

times

just

as

you

want,

the

n

means

the

min

and

the

m

meansthe

max

times/x{2,10}/

match

x

from

2

to

10

timesCharacter

classUse

[

]

to

contain

the

regular

express1.[abcde][a-e][gG][0-9]5.

[0-9]+6.

[A-Za-z]anyone

in

a,b,c,d,esame

to

1g

or

Gany

numberone

or

more

numbersany

word7.

[*!@#$%&()]

anyone

of

these

signals“^”

/[^abcde]/matches

anyone

excepta,b,c,d,e/[\^abcde]/

matches

^,a,b,c,d

or

eSpecial

character

class\w,

\W\d,

\D\s,

\SGroup

and

SelectionSelection/dogs|cats/

:

match

dogs

or

catsGroup

If

want

to

match

frog,

bog,

log,

flog

and

clog,

youcan

use

:/frog|bog|flog|flog|clog/,

if

use/fr|b|fl|clog/

maybe

wrongUse

(

)

in

the

regular

expression

as

a

group/(fr|b|fl|cl)og/,

which

equals

to

/frog|bog|flog|flog|clogLocation

wildcard“^”

Insert

“^”

into

the

head

of

the

regularexpression,

you’ll

match

something

which

beginas

the

regular/^head/

will

match

“head

sfas

a;jfajgfeolej”“$”

Insert

“$”

at

the

end

of

the

regular

expressionmatch

something

end

as

the

regular

expression/end$/

matches

“sddjsagkl

segfekend”Substitute

and

regularoperatorSubstitues/searchpattern/replacement/;search

the

default

variable

$_,

find

the

reachpattern

andreplace

it

with

replacementi.e.:

$_=“Our

house

in

the

middle”;s/middle/end/;s/in/at/;

-end”;Regular

operator“=~”$a=“hello”;$_

now

equals

“Our

house

at

the$a=~/el/;

$a=~s/el/ll/;-$a=“hlllo”;File

operationRead

from

keybord$a=<STDIN>;Read

from

input

parameters@ARGV,

each

@ARGV

is

seperated

by

spaceRead

from

file Open(filehandle,”filename”); @a=<filehandle>;close(filehandle);Write

to

fileOpen(filehandle,”>filename”);Print

filehandle

“what

you

want

to

write”;close(filehandle);Delete

fileUnlink

“filename”;array

operationPrint

and

printfPrint

:print

general

information,

use

default

formatPrintf:

print

formated

information,

the

format

is

the

samelike

CStack

operationPop

target_array

:

pop

from

top

of

stackPush

target_array,new_list

:

push

into

top

of

stackShift

target_array:

push

into

the

end

of

stackUnshift

target_array,new_list:

pop

from

the

end

of

stackDirectory

operationDirectory

operationmkdir

directory,

0xxx

:

build

a

directoryopendir

dirhandle,

directoryreaddir,dirhandleClosedir(dirhandle)Glob

patternJust

as

command

shell

:

lsLs

*.*

==glob(*.*)Directory

operationExplore

direcoryChange

current

directoryChdir

newdir;Delete

dirRmdir

pathname;

#only

delete

empty

dirChange

name

for

fileRename

oldname,newname;Glue

other

programsThe

most

important

function

of

perl

Integrate

many

programs

into

a

whole

to

deal

witha

job

which

may

be

difficult

to

do

by

ordinaryprogramsCan

combing

GMT,

SAC,

TTIMES,

PASSCAL,PSSACand

programs

written

by

C++,

fortran,

etc

Combing

with

regular

rule

perl

can

deal

withalmost

all

kinds

work

automaticallyThe

usage

of

calling

otherprogramsCall

programs

without

output1.

Open

(filehandle,

“|prog_name”)2.

Print

filehandle

“whatever

you

want”3.

Close(filehandle)Call

programs

with

output1.

Open

(filehandle,

“|prog_name>file”)2.

Print

filehandle

“whatever

you

want”3.

Close(filehandle)Call

system

commands`system

commands`;Perl

functionBuild

a

functionSub

subroutine_name{statement1;:statementx;}ExampleSub

yesno{print

“Are

yousure(Y/N)\n”;$anser=<STDIN>;}Call

a

subroutine

Call

a

subroutinewithout

parametersinteraction&sub_name();Sub_name();ExampleSub

countdown{for($i=10;$i>=0;$i--){print

“$i

–”;}}print

“T-minus:

“;cuntdown();print

“Blastoff”;Call

a

subroutine

withparameters

interactionReturn

value1.

default

valueReturn

the

value

of

the

last

expression

in

the

sub2.

Return

allocate

valueReturn(para1,para2,….paraN);Return

typesReturn(@array)Return(%Hash)Return($scalar)paramtersSend

parameters

to

subroutine1.

Subname(arg1,…argn);2.

Subname

arg1,…argn;3.

&subname(arg1,…argn);Get

parameter

from

calling

program1.

@_;sub

printargs(print

join(‘,’,@_);}2.

From

pointersSome

useful

functionsGrepGrep

BLOCK

LISTGrep

expr,listSubstrSubstr

Expr,offset,lengthSubstr

expr,offsetIndexIndex

string,substringIndex

string

substring,

start_positionSplitSplit

/pattern/,expr,limitSplit

/pattern/,exprJoinJoin

expr,list #

$a1[0]=“I”;$a1[1]=“am”;join(‘

‘,@a1);Qw,

chomp

#qw(I

am

a

Chinese);Use

external

blockPerl

is

an

open

software

Now

more

than

3500

blocks

have

beendeveloped

by

programmersSome

useful

block1.

Cwd2.

Strict3.

File

#include

many

file

operations4.

Net

#include

many

protocols

used

on

WEBNet::PingA

Detail

Perl

Program

This

program

is

developed

by

me

toextract

explosion

data

by

CRG

group,

asthe

original

data

is

too

long

and

theseed

file

do

not

contain

any

eventinformation,

here

I

add

the

explosioninformation

to

the

extracted

SAC

files,as

well

as

the

information

of

stationazimuth,

distance,

and

event

happenedtimeOriginal

data

of

huabeiexperimentData

formatCTN,

just

the

same

as

seed

file

In

CTN

header,

contains

station

responseparameters,

the

zeroes

and

the

poles

In

original

SAC

files,

contain

locationinformation

of

stations

We

need

a

event

time

file

which

containstime

and

source

locationTxt

formatn12005,355,16:30:07:584

(year.month.day.hour:minute:second:ms)n22005,355,16:30:27:874n32005,355,16:30:48:070n42005,355,16:31:08:167n52005,355,16:31:29:062n62005,355,16:31:45:860n72005,355,16:32:11:555n82005,355,16:32:29:952n92005,355,16:32:46:550n102005,355,16:33:07:344n112005,355,16:33:29:241n122005,355,16:33:46:138n132005,355,16:34:07:733n142005,355,16:34:25:031n152005,355,16:34:47:227n162005,355,16:35:07:524source

116.52853

40.18603Perl

program#this

program

is

used

to

read

seed

file

from

IRIS

WilbertII#in

this

program

I

make

directory

for

each

seed

file

and

maketheir

name

shorter

than

original"s#and

also,

I

change

the

head

files

of

the

extracted

SACs

to

theevent

time.#this

program

was

edited

by

Zhengyong,2006/1/4/#most

of

the

ideas

in

this

program

come

from

Prof.

NSD"s

shellscript#because

the

data

for

the

explosion

has

not

az,baz

and

gcarc

inthe

header,#here

I

add

them

into

the

SAC

file

by

the

location

of

source

andstations#the

time

file

contains

source

times

and

locationuse

strict;my

$usage="Error!

the

correct

usage

is

:\nperlextract_explosion.pl

-Ftime_file

-Oout_dir\n";@ARGV>1

or

die

"$usage\n";my(@seed_files,$origin_time,@origin,$seed_files);my($tmp1,$tmp2,$tmp3,$tmp4);my

($x,@aa,@bb);my

$time_file;my

($seed_dir,$out_dir);#event

location#azimuth

and

gcarc(distance)#station

locationmy

($evla,$evlo);my

($az,$gcarc,$dist);my

($stla,$stlo);my

$tmp;#my用于定义变量■■■■■■foreach(grep(/^\-/,@ARGV)){my

$opt=substr($_,1,1);my

$value

=

substr($_,2);if($opt

eq

"F"){

$time_file=$value;}elsif($opt

eq

"O"){$out_dir=$value;}else{print

"usage\n";}}`ls

-1

*.CTN>1.txt`;

open(CTN,"1.txt")

or

die

"cannot

open

seed

file

name\n";@seed_files=<CTN>;close(CTN);#unlink

"1.txt";chomp

@seed_files;mkdir($out_dir,0777)

unless

-d$out_dir;

#build

directory

forsave

SAC

filesmy

$seeddir=$out_dir;■■■■■

open(ORIGIN,$time_file)

or

die"can

not

open

file

of

explosiontime\n";@origin=<ORIGIN>;chomp

@origin;close(ORIGIN);

#pick

out

the

location

and

origintime

of

eventforeach

(@origin){if(/n\d+\s+\d+\,\d+/){

($tmp1,$origin_time)=split(/\s+/,$_);print"$origin_time\n";last;}}■■■■■foreach(@origin){if(/source/){($tmp1,$evlo,$evla)=split(/\s+/,$_);last;}}#read

each

seed

files

and

deal

with

them

one

by

oneforeach

$seed_files

(@seed_files){#

open(ORIGIN,"$seed_");■my

$tmp_input;#bulid

a

directory

for

each

seed

file■■#

my

$seeddir;#

($seeddir)=split(/\./,$seed_files);#

mkdir($seeddir,0777)

unless

-d

$seeddir; @aa=split(/\,/,$origin_time); @bb=split(/:/,$aa[2]);■open(SAC_MACRO,">$seeddir/$seed_files.cho.sm")

or

die

"can

not

open

cho.sm\n";print

SAC_MACRO

"r

\$1\n";■■print

SAC_MACRO

"chnhdr

o

gmt

$aa[0]

$aa[1]$bb[0]

$bb[1]

$bb[2]

$bb[3]\n";print

SAC_MACRO

"evaluate

to

tt1

&1,o

*

-1\n";print

SAC_MACRO

"chnhdr

allt

\%tt1\n";print

SAC_MACRO

"w

over\n";print

SAC_MACRO

"cut

-100

2000\n";print

SAC_MACRO

"r

\$1\n";print

SAC_MACRO

"w

over\n";close(SAC_MACRO);■■■■■■■■■■■■system("/opt/util/rdseed

-df

$seed_files"system("/opt/util/rdseed

-pf

$seed_files"

#now

remove

the

lengthy

prefix(date+time)for

each

station

to#make

the

name

shorter system("ls

-1

*.SAC>tmp");system("ls

-1

SAC_*>tmp2");open(FILETMP,"tmp")

or

die

"can

notopen

file

tmp\n";open(FILETMP2,"tmp2")

or

die

"can

notopen

file

tmp\n";■■■■my

(@sacfiles,$sacfiles);@sacfiles=<FILETMP>;chomp

@sacfiles;my

(@res_sacfiles,$res_sacfiles);

#response

file@res_sacfiles=<FILETMP2>;chomp■■■■■■@res_sacfiles;close(FILETMP);close(FILETMP2);foreach

$sacfiles

(@sacfiles){ my

@aa2;@aa2=split(/\./,$sacfiles);system("mv

$sacfiles$seeddir/$aa2[7].$aa2[8].$aa2[9]");}■system("ls

-1

$seeddir/\*\.[BS]H?>2.tmp");my

(@bhfiles,$bhfiles);open(BHFILE,"2.tmp");@bhfiles=<BHFILE>;chomp■■■@bhfiles;close(BHFILE);unlink

"2.tmp";foreach

$bhfiles

(@bhfiles){open(SAC,"|/opt/sac/bin/sac100")or

die

"can

notexecute

SAC100\n";print

SAC

"r

$bhfiles\n";print

SAC

"m

$seeddir/$seed_files.cho.sm$bhfiles\n";close(SAC);■■■■■■#now

begin

to

pick

out

the

station

locationopen(SAC,"|/opt/sac/bin/sac100>stloc.txt")or

die

"can

not■execute

SAC100\n";■print

SAC

"r

$bhfiles\n";print

SAC

"lh

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论