`

perl the three dot operator

    博客分类:
  • perl
阅读更多

In my previous post, I discussed the range operator in the scalar context, here we are further to discuss on operator tha is quite unique in the Perl programming lanaugae, that is the three dot operator. 

 

... 

 

 

Some of the examples are referenced from the stackover post  http://stackoverflow.com/questions/11021512/perl-three-dot-operator-examples

 

 

Let's first see an example and then let 's delve into it.

 

 

use strict;


my @lines = ("   - Foo",
          "01 - Bar",
          "1  - Baz",
          "   - Quux"
);

# the following command will print out Bar

foreach (@lines) {
    if (/0/ .. /1/) {
        print "$_\n";
    }
}


# but the following comman will print out Baz and as well as Bar


print "=" x 10, "\n"; 

foreach (@lines) {
  if (/0/ ... /1/) { 
     print "$_\n";
   }
}

 

 

The output of the script will be like this: 

 

 写道
01 - Bar
==========
01 - Bar
1 - Baz
 

 

Let's first see the definition from one tutorial page: it has the following.

 

http://www.ooblick.com/text/perl/perl-talk1-text.html 写道
.. (dot dot)
The range operator is rather magical. It's one of my favorites.

In a list context, num1..num2 returns the list of numbers from num1 to num2. This is handy for taking array slices (@array[3..10]) or for repeating a loop a fixed number of times (for (1..50)...). Be aware, though, that this does generate a temporary array, so you can waste a lot of memory by using this with a large range.

In a scalar context, expr1 .. expr2 acts as a ``flip-flop.'' It starts out false. Then, once the left-hand expression becomes true, .. returns true and starts evaluating its right-hand expression, until that becomes true. After that, .. flip-flops back to being false.

This is useful for things like finding text between delimiters in a file:

perl -ne 'print if /BEGIN/../END/'
will read standard input and print all lines between lines delimited by BEGIN and END.

There is also a ... (dot dot dot) operator, related to .., but I won't cover it here.
 

The main reason is explained in the stackover flow post. Here is what it said.

 

 

http://stackoverflow.com/questions/11021512/perl-three-dot-operator-examples 写道
«...» doesn't do a flop check immediately after a true flip check.

With «..»,

" - Foo"
/0/ returns false.
.. returns false.
"01 - Bar"
/0/ returns true. Flip!
/1/ returns true. Flop! ⇐
.. returns true (since the first check was true).
"1 - Baz"
/0/ returns false.
.. returns false.
" - Quux"
/0/ returns false.
.. returns false.
With «...»,

" - Foo"
/0/ returns false.
... returns false.
"01 - Bar"
/0/ returns true. Flip!
... returns true.
"1 - Baz"
/1/ returns true. Flop!
... returns true.
" - Quux"
/0/ returns false.
... returns false.
 
分享到:
评论

相关推荐

    SAM分割模型onnx导出模型问题:Exporting the operator repeat-interleave to ON

    Exporting the operator repeat_interleave to ONNX opset version 11 is not supported TypeError: 'torch._C.Value' object is not iterable (Occurred when translating repeat_interleave). 问题解决: 1....

    Infancechnifra.zip_Infrared enhance_The Operator_infrared image

    Infrared image enhancement is a crucial technique for improving the ...operator is given, and the multi-scale extension of the feature extraction is discussed in details. Finally, the extracted final

    The skyline Operator

    title = {Proceedings of the 17th International Conference on Data Engineering, April 2-6, 2001, Heidelberg, Germany}, publisher = {IEEE Computer Society}, year = {2001}, isbn = {0-7695-1001-9}, ...

    bioinforamtics perl resources

    # An alternative way using the "dot operator": # Concatenate the DNA fragments into a third variable and print them $DNA3 = $DNA1 . $DNA2; print "Here is the concatenation of the first two fragments ...

    Note on the estimate for the second eigenvalue of the Jacobi operator of hypersurfaces in a sphere

    关于球面中超曲面上的Jacobi算子的第二特征值的估计的一个注记,陈航,王险峰,李-王研究了球面中具有常数量曲率的超曲面的雅可比算子的第二特征值。之后,陈-王进一步研究了球面中的魏恩加滕超曲面的保体积变分

    LBP.rar_The Operator

    Local Binary Pattern (LBP) operator it is possible to describe the texture and shape of a digital (gray scale) image. One LBP is a binary code for an image -pixel which tells something about the ...

    chaosblade-operator镜像

    "chaosblade-operator镜像" 是一个专门针对混沌工程实验的 Kubernetes 操作器,它用于在 Kubernetes 集群中安全、可控地引入故障,以评估和增强系统的容错能力。混沌工程是一种通过主动引入故障来验证系统稳定性的...

    new操作符(new operator)和operator new的区别

    在C++编程中,`new`操作符和`operator new`是两个密切相关但不完全相同的概念,它们在内存管理和对象创建方面各自承担着特定的角色。 `new`操作符是C++语言内置的关键字,用于在堆上动态分配内存并创建对象。当执行...

    VC.data.operator.overloading.rar_The Operator

    "VC.data.operator.overloading.rar_The Operator"这个资源很可能包含了一些关于在Visual C++环境下如何有效地使用和实现运算符重载的经典教程和示例。 运算符重载并不创建新的运算符,而是给已有的运算符赋予新的...

    operator_C++_

    7. **赋值运算符的特殊性**:重载赋值运算符(`=`)时,通常需要遵循“浅复制”和“深复制”的原则,以防止意外的共享状态,这有时被称为“拷贝构造函数和赋值运算符的‘Rule of Three’”。 8. **异常安全**:在...

    Data Cube: A Relational Aggregation Operator

    The SQL aggregate functions and the GROUP BY operator produce zero-dimensional or one-dimensional answers. Applications need the N-dimensional generalization of these operators. This paper defines ...

    operator-helloworld.zip

    在Kubernetes(简称k8s)生态系统中,Operator是一种高级的资源管理技术,它扩展了Kubernetes的能力,允许开发者定义和管理复杂的应用服务。"operator-helloworld.zip" 提供了一个入门级的Operator示例,帮助用户...

    canny_edge_detection.rar_The Operator

    The Canny edge detector is an edge detection operator that uses a multi-stage algorithm to detect a wide range of edges in images.

    kubernetes学习记录(15)——使用operator-sdk开发operator

    operator-sdk环境搭建 我的电脑环境为windows,安装operator-sdk需要自己编译构建二进制文件。 参考官方文档Compile and install from master 官方给的Prerequisites为: git mercurial version 3.9+ bazaar version...

    Reminiscences of a Stock Operator

    Reminiscences of a Stock Operator by Edwin Lefevre, 75th anni. edition

    Prometheus-Operator.pdf

    Prometheus Operator是一个工具,用于简化在Kubernetes平台上部署和管理Prometheus实例的操作。通过将Prometheus与其监控的目标关联起来,并在目标发生变化时自动更新配置,Prometheus Operator大大简化了Prometheus...

    cassandra-operator,apache-cassandra的kubernetes算子.zip

    Cassandra-Operator是针对Apache Cassandra在Kubernetes集群中部署和管理的一个开源项目。它使得在Kubernetes环境中运行和扩展Cassandra数据库变得更加简单和自动化。在这个压缩包“cassandra-operator,apache-...

    Least absolute shrinkage and selection operator(LASSO)

    LASSO是由1996年Robert Tibshirani首次提出,全称Least absolute shrinkage and selection operator。该方法是一种压缩估计。它通过构造一个惩罚函数得到一个较为精炼的模型,使得它压缩一些回归系数,即强制系数...

Global site tag (gtag.js) - Google Analytics