Perl语言入门和提高03

Perl语言入门和提高03

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Spring, 2009 1 Perl 入门和提高 Lesson 3 周晓方 courses@xfzhou.homeftp.org Spring, 2009 2 Sample @s 里面是一个 spice 文本,请把电容的容量加倍, 电阻的阻值减半;不考虑续行。 提示,电容卡的格式是 " C 名称 Node1 Node2 容量 " @s = ('RI N 12 10MEG', 'EGAI N 3 0 1 2 100K', 'RP 1 34 1K', 'CP 1 40 1.5915UF', 'ROUT 56 10'); s/^(C(\S+\s+){3})([0-9\.]+)/$1.($3*2)/e foreac h @s; /^R/ and s/([0-9\.]+)(\S*)$/($1\/2).$2/e foreac print join "\n", @s; RIN 1 2 5MEG EGAIN 3 0 1 2 100K RP1 3 4 0.5K CP1 4 0 3.183UF ROUT 5 6 5 别忘了除法运算符 / 要转义 Spring, 2009 3 join, map, split, reverse, sort, grep join("\n", @array) ; # return string map("$_\n", @array) ; # return array print join "\t", map $_*$_, 1. .10; print join "\t", map int rand(20), 1. .10; split(/pattern/, "string") ; # return array @list = split(//, "ABCDEFGHI"); # qw(A B C D E F G H I) @list = split(/:/, "12:34:56:78", 3); # qw(12 34 56:78) reverse(@array) ; # return array 数组反序 scalar reverse(@array) ;# 合并成字符串,字符反序 %new_hash = reverse %old_hash;# 交换哈希表的 key val sort(@array) ; # return array sort {$a cmp $b} @list ; # 用法很丰富,参考 perlop sort {$a <=> $b} @list; sort {-($a <=> $b)} @list; grep(/pattern/, @array) ; # return filtered array @foo = grep(!/^#/, @bar) ; # remove lines start with # Spring, 2009 4 Hash 表用于计数 单词统计 !/usr/bin/perl -w use strict; my @lines = <DATA> ; # Read in data chomp @lines ; # Remove CRLF my $line = lc join " ", @lines; $line =~ s/[^a-z]+/ /g ; # remove ,:.()' my @words = split / /, $line; my @chars = split //, $line; print "=====List of all words:\n"; print join "\t", @words; my ($word, $char, $count, %words, %chars); $words{$_}++ foreach @words; $chars{$_}++ foreach @chars; print "\n\n=====Word count:\n"; print "$word\t$count\n" while ($word, $count) = each %words; print "\n\n=====Char count:\n"; print "$char\t$count\n" while ($char, $count) = each %chars; 1; __END__ There is one minor difference: if variables are declared with my in the initialization section of the for, the lexical scope of those …… =====Word count: the 9 test 1 explicitly 1 you 2 normal 1 file 2 exactly 1 that 1 my 1 …… =====Char count: w 1 a 27 r 26 x 4 d 11 y 9 u 4 h 16 …… Spring, 2009 5
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/27/2011 for the course CS Perl taught by Professor Guo during the Spring '09 term at Xiamen University.

Page1 / 2

Perl语言入门和提高03

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online