substr EXPR,OFFSET,LENGTH,REPLACEMENT
substr EXPR,OFFSET,LENGTH
substr EXPR,OFFSET
Extracts a substring out of EXPR and returns it. First character
is at offset zero. If OFFSET is negative, starts that far back
from the end of the string. If LENGTH is omitted, returns
everything through the end of the string. If LENGTH is negative,
leaves that many characters off the end of the string.

my $s = "The black cat climbed the green tree";
my $color = substr $s, 4, 5; # black
my $middle = substr $s, 4, -11; # black cat climbed the
my $end = substr $s, 14; # climbed the green tree
my $tail = substr $s, -4; # tree
my $z = substr $s, -4, 2; # tr

You can use the "substr" function as an lvalue, in which case EXPR
must itself be an lvalue. If you assign something shorter than
LENGTH, the string will shrink, and if you assign something longer
than LENGTH, the string will grow to accommodate it. To keep the
string the same length, you may need to pad or chop your value
using "sprintf".

If OFFSET and LENGTH specify a substring that is partly outside
the string, only the part within the string is returned. If the
substring is beyond either end of the string, "substr" returns the
undefined value and produces a warning. When used as an lvalue,
specifying a substring that is entirely outside the string raises
an exception. Here's an example showing the behavior for boundary
cases:

my $name = 'fred';
substr($name, 4) = 'dy'; # $name is now 'freddy'
my $null = substr $name, 6, 2; # returns "" (no warning)
my $oops = substr $name, 7; # returns undef, with warning
substr($name, 7) = 'gap'; # raises an exception

An alternative to using "substr" as an lvalue is to specify the
replacement string as the 4th argument. This allows you to replace
parts of the EXPR and return what was there before in one
operation, just as you can with "splice".

my $s = "The black cat climbed the green tree";
my $z = substr $s, 14, 7, "jumped from"; # climbed
# $s is now "The black cat jumped from the green tree"

Note that the lvalue returned by the three-argument version of
"substr" acts as a 'magic bullet'; each time it is assigned to, it
remembers which part of the original string is being modified; for
example:

my $x = '1234';
for (substr($x,1,2)) {
$_ = 'a'; print $x,"\n"; # prints 1a4
$_ = 'xyz'; print $x,"\n"; # prints 1xyz4
$x = '56789';
$_ = 'pq'; print $x,"\n"; # prints 5pq9
}

With negative offsets, it remembers its position from the end of
the string when the target string is modified:

my $x = '1234';
for (substr($x, -3, 2)) {
$_ = 'a'; print $x,"\n"; # prints 1a4, as above
$x = 'abcdefg';
print $_,"\n"; # prints f
}

Prior to Perl version 5.10, the result of using an lvalue multiple
times was unspecified. Prior to 5.16, the result with negative
offsets was unspecified.

perl substr的更多相关文章

  1. 如何用Perl对Excel的数据进行提取并分析

    巡检类工作经常会出具日报,最近在原有日报的基础上又新增了一个表的数据量统计日报,主要是针对数据库中使用较频繁,数据量又较大的31张表.该日报有两个sheet组成,第一个sheet是数据填写,第二个sh ...

  2. perl脚本基础总结

    1.  单引号字符串中的\n不会被当做换行符处理. 如:'\'\\'  -->  '\  . 2.  双引号 字符串联    "Hello"."World" ...

  3. perl基本语法--转载

    http://www.cnblogs.com/zhtxwd/archive/2012/03/06/2381585.html 本文介绍从变量类型.操作运算符.控制叙述.子程序.I/O和档案处理. Reg ...

  4. [Perl]抓取个人的所有闪存+格式化保存为文本

    以下代码保存为utf8文本格式 环境:ActivePerl v5.16 built for MSWin32-x86 两个要调整的地方: for my $i (17..45) {  这里改成自己对应的页 ...

  5. [转载]两个半小时学会Perl

    Learn Perl in about 2 hours 30 minutes By Sam Hughes Perl is a dynamic, dynamically-typed, high-leve ...

  6. 一些Perl例程(全部手打并执行过)

    #-1-变量使用,打印#!/usr/local/bin/perl$value=100+30+3+8;print("Value=",$value,"\n"); # ...

  7. Perl的基本语法<总结> (转载)

    前言:这篇文章是花了我很多时间.费了我很多心血才完成的,虽然连我自己都觉得无法达到尽善尽美的境界,但希望能帮助大家入门,稍微了解到Perl 到底是个什么样的东西,Perl到底有那些强大的功能,那么这篇 ...

  8. (转载)CSV 文件处理 PERL

    http://cn.perlmaven.com/how-to-read-a-csv-file-using-perl http://search.cpan.org/~hmbrand/Text-CSV_X ...

  9. Perl 语法 - 高级特性

    总结: q().qq().qw(同单引号).qx{牢记是花括号},分别是单引号.双引号.创建字符串列表 和 捕获命令输出.   第9学时 其他函数和运算符 一件事情可以使用多种方法完成. 有哪些其他的 ...

随机推荐

  1. 正则表达式: NFA引擎匹配原理

    NFA引擎匹配原理 1       为什么要了解引擎匹配原理 一个个音符杂乱无章的组合在一起,弹奏出的或许就是噪音,同样的音符经过作曲家的手,就可以谱出非常动听的乐曲,一个演奏者同样可以照着乐谱奏出动 ...

  2. cm2 逆向分析

    目录 cm2 逆向分析 前言 查壳 逆向分析 encrypt函数 POC代码 cm2 逆向分析 前言 这是逆向实战之CTF比赛篇的第3篇,在这里我就不再讲的特别小白了,有些简单操作可能会略过. 查壳 ...

  3. windows端口占用处理方法

    (1)输入命令:netstat -ano,列出所有端口的情况.在列表中我们观察被占用的端口,比如是8081,首先找到它.C:\Users\Administrator>netstat -ano活动 ...

  4. loadRunner12 设置关联 获取服务端动态数据

    关联:服务器返回给客户端一些动态变化的值,客户端用这些值去访问服务器,不能把这些值写死在脚本里面,而应该存放在一个变量里面. 在脚本回放过程中,客户端发出请求,通过关联函数所定义的左右边界值(也就是关 ...

  5. String你会用吗?

    1. 如果不是在循环体中进行字符串拼接的话,直接使用 String 的 "+" 就好了. 2. 单线程循环中操作大量字符串数据 → StringBuilder.append() 3 ...

  6. 【JAVA】笔记(4)---继承;方法覆盖;多态机制;super;

    继承(extends): 1.作用:为方法覆盖和多态机制做准备:使代码得到复用(节省代码量): 2.格式: class 子类 extends 父类 3.理解继承:子类继承父类,其实就相当于把父类的类体 ...

  7. Java核心技术--Java程序设计

    Java术语 术语名 缩写 解释 Java Development Kit(Java开发工具包) JDK 编写Java程序的程序员使用的软件 Java Runtime Environment(Java ...

  8. [cf1290D]Coffee Varieties

    思路 统计数的种类数,也等价于统计有多少个数满足其之前没有与其相同的数 将序列以$\frac{k}{2}$为块大小分块,那么即会有$m=\frac{2n}{k}$个块 (关于$k=1$的情况,以1为块 ...

  9. [nowcoder5667K]Keyboard Free

    不妨设$r1\le r2\le r3$,令$f(\alpha)=E(S_{\Delta}ABC)$,其中AB坐标分别为$(r_{1},0)$和$(r_{2}\cos \alpha,r_{2}\sin ...

  10. [bzoj1107]驾驶考试

    转化题意,如果一个点k符合条件,当且仅当k能到达1和n考虑如果l和r($l<r$)符合条件,容易证明那么[l,r]的所有点都将会符合条件,因此答案是一个区间枚举答案区间[l,r],考虑如何判定答 ...