perl substr
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的更多相关文章
- 如何用Perl对Excel的数据进行提取并分析
巡检类工作经常会出具日报,最近在原有日报的基础上又新增了一个表的数据量统计日报,主要是针对数据库中使用较频繁,数据量又较大的31张表.该日报有两个sheet组成,第一个sheet是数据填写,第二个sh ...
- perl脚本基础总结
1. 单引号字符串中的\n不会被当做换行符处理. 如:'\'\\' --> '\ . 2. 双引号 字符串联 "Hello"."World" ...
- perl基本语法--转载
http://www.cnblogs.com/zhtxwd/archive/2012/03/06/2381585.html 本文介绍从变量类型.操作运算符.控制叙述.子程序.I/O和档案处理. Reg ...
- [Perl]抓取个人的所有闪存+格式化保存为文本
以下代码保存为utf8文本格式 环境:ActivePerl v5.16 built for MSWin32-x86 两个要调整的地方: for my $i (17..45) { 这里改成自己对应的页 ...
- [转载]两个半小时学会Perl
Learn Perl in about 2 hours 30 minutes By Sam Hughes Perl is a dynamic, dynamically-typed, high-leve ...
- 一些Perl例程(全部手打并执行过)
#-1-变量使用,打印#!/usr/local/bin/perl$value=100+30+3+8;print("Value=",$value,"\n"); # ...
- Perl的基本语法<总结> (转载)
前言:这篇文章是花了我很多时间.费了我很多心血才完成的,虽然连我自己都觉得无法达到尽善尽美的境界,但希望能帮助大家入门,稍微了解到Perl 到底是个什么样的东西,Perl到底有那些强大的功能,那么这篇 ...
- (转载)CSV 文件处理 PERL
http://cn.perlmaven.com/how-to-read-a-csv-file-using-perl http://search.cpan.org/~hmbrand/Text-CSV_X ...
- Perl 语法 - 高级特性
总结: q().qq().qw(同单引号).qx{牢记是花括号},分别是单引号.双引号.创建字符串列表 和 捕获命令输出. 第9学时 其他函数和运算符 一件事情可以使用多种方法完成. 有哪些其他的 ...
随机推荐
- 单片机stm32的5个时钟源的详细分析
众所周知STM32有5个时钟源HSI.HSE.LSI.LSE.PLL,其实他只有四个,因为从上图中可以看到PLL都是由HSI或HSE提供的. 其中,高速时钟(HSE和HSI)提供给芯片主体的主时钟.低 ...
- 关于STM32 (Cortex-M3) 中NVIC的分析
一.STM32 (Cortex-M3) 中的优先级概念 STM32(Cortex-M3)中有两个优先级的概念:抢占式优先级和响应优先级,也把响应优先级称作"亚优先级"或" ...
- 修改 oracle 数据库的 sys 账号密码,ERROR at line 1: ORA-01034: ORACLE not available
挺久没有登录的 oracle 数据库,因为公司要求加固密码,登录后修改失败 1.启动数据库的同时启动控制文件.数据文件,提示:cannot mount database in EXCLUSIVE mo ...
- Redis安装、配置和卸载
1.安装 mkdir /usr/local/redis 添加目录 wget [http://download.redis.io/releases/redis-4.0.1](http://downloa ...
- git远程仓库、提交代码操作
初始化仓库 1.初始化 git init #或 git clone 远程仓库地址 git init 后续要添加远程仓库,git clone不需要再添加 2.连接仓库 git remote add 远程 ...
- PTA 7-1 是否完全二叉搜索树 (30分)
PTA 7-1 是否完全二叉搜索树 (30分) 将一系列给定数字顺序插入一个初始为空的二叉搜索树(定义为左子树键值大,右子树键值小),你需要判断最后的树是否一棵完全二叉树,并且给出其层序遍历的结果. ...
- C语言二分查找法
参考了C语言中折半查找法(二分法)的实现 二分查找算法(C语言实现) 先附上代码 #include<stdio.h> int BinSearch(int arr[],int len,int ...
- 问题 L: Yougth的最大化
题目描述 Yougth现在有n个物品的重量和价值分别是Wi和Vi,你能帮他从中选出k个物品使得单位重量的价值最大吗? 输入 有多组测试数据 每组测试数据第一行有两个数n和k,接下来一行有n个数Wi和V ...
- 深入理解Spring IOC容器及扩展
本文将从纯xml模式.xml和注解结合.纯注解的方式讲解Spring IOC容器的配置和相关应用. 纯XML模式 实例化Bean的三种方式: 使用无参构造函数 默认情况下,会使用反射调用无参构造函数来 ...
- 大爽Python入门教程 0-2 什么是IDE?python选什么IDE好?
大爽Python入门公开课教案 点击查看教程总目录 一 感受IDE 什么是IDE? 在这里,我并不想直接给出一个回答, 因为这个回答对初学者来说,可能有些抽象. 我想先带大家感受下IDE. 1 比较不 ...