string – 从perl变量中删除空格
发布时间:2020-12-15 21:50:04 所属栏目:大数据 来源:网络整理
导读:我在进行简单的搜索和替换方面遇到了很多麻烦.我试过提供的解决方案 How do I remove white space in a Perl string? 但无法打印出来. 这是我的示例代码: #!/usr/bin/perluse strict;my $hello = "hello world";print "$hellon"; #this should print out h
我在进行简单的搜索和替换方面遇到了很多麻烦.我试过提供的解决方案
How do I remove white space in a Perl string? 但无法打印出来. 这是我的示例代码: #!/usr/bin/perl use strict; my $hello = "hello world"; print "$hellon"; #this should print out >> hello world #now i am trying to print out helloworld (space removed) my $hello_nospaces = $hello =~ s/s//g; #my $hello_nospaces = $hello =~ s/hello world/helloworld/g; #my $hello_nospaces = $hello =~ s/s+//g; print "$hello_nospacesn" #am getting a blank response when i run this. 我尝试了几种不同的方法,但我无法做到这一点. 我的最终结果是自动化在Linux环境中移动文件的某些方面,但有时文件名称中有空格,所以我想从变量中删除空格. 解决方法
你快到了;你只是对操作符优先级感到困惑.您要使用的代码是:
(my $hello_nospaces = $hello) =~ s/s//g; 首先,这将变量$hello的值赋给变量$hello_nospaces.然后它会对$hello_nospaces执行替换操作,就像你说的那样 my $hello_nospaces = $hello; $hello_nospaces =~ s/s//g; 因为绑定运算符=?的优先级高于赋值运算符=,所以编写它的方式 my $hello_nospaces = $hello =~ s/s//g; 首先在$hello上执行替换,然后将替换操作的结果(在本例中为1)分配给变量$hello_nospaces. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |