Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 将空格替换为0_Perl_Substitution - Fatal编程技术网

Perl 将空格替换为0

Perl 将空格替换为0,perl,substitution,Perl,Substitution,使用Perl,我只想将空格替换为0。空格由制表符(\t)分隔。提前谢谢!例如: 1 2 2 5 4 4 4 4 4 3 4 4 1 1 5 6 4 到 我的代码: use strict; use warnings; open(DATA,

使用Perl,我只想将空格替换为0。空格由制表符(\t)分隔。提前谢谢!例如:

1   2           2       5               4
4   4   4           4               3   
        4   4           1               
    1   5   6       4                   

我的代码:

use strict;
use warnings;
open(DATA,"DATA")||die"cannot open the file: $!\n";


while( <DATA> )
  {
  s/(^|    \K)(?!\d)/0/g;
  print;
  }
使用严格;
使用警告qw(全部);
使用特征qw(例如);
而(){
咀嚼;
my@fields=split(/\t/,$\u1);
对于我的$field(@fields){
如果$field eq“”,则$field=0;
}
说出join“\t”、@字段;
}
不清楚你所说的“空间”是什么意思。以上内容将空字段替换为零。选择以下最合适的选项:

  • 如果$field eq”“
    (空)
  • 如果$field eq”“
    (1个空格)
  • 如果$field=~/^[]+\z/
    (1+空格)
  • 如果$field=~/^[]*\z/
    (0+空格)
  • 如果$field=~/^\s+\z/
    (1+空格)
  • 如果$field=~/^\s*\z/
    (0+空格)
非常简单, 只需将文件内容存储在变量$x中,然后找到匹配项并替换:

use strict;
use warnings;
my $filename = "c:\path\to\file.txt";
my $x;
    open(my $fh, '<', $filename) or die "cannot open file $filename: $!";
    {
        local $/;
        $x= <$fh>;
    }
    close($fh);


$x=~s/(\n )/\n0/g;      #starting zeros
$x=~s/( \n)/ 0\n/g;     #ending zeros
$x=~s/( $)/ 0\n/g;      #last zero if no end line on end of string
$x=~s/(^ )/0/g;         #first zero at beginning of string
$x=~s/(    )/   0/g;    #zeros within the matrix

print $x;
使用严格;
使用警告;
my$filename=“c:\path\to\file.txt”;
我的$x;

打开(my$fh),如空格字符?这是什么语言?例如,1和2之间的字符是什么?是制表符(\t)吗?谢谢,Alan Deep。是制表符(\t)。提示:不要使用
数据
;它是现有文件句柄的名称。提示:不要对文件句柄使用全局变量。使用词法变量(
打开(my$DATA)
)@yueli我从未使用过perl,但我阅读了文档来回答您的问题。
$filename
应替换为
'temp01'
。如果temp01位于
C:///code>中,则将$filename替换为
'C:/temp01'
你好,Alan Deep,非常感谢您的快速响应。我替换了文件temp01。但是,它出来了:Global symbol“$temp01”要求在alan.pl第4行有明确的包名。alan.pl的执行由于编译错误而中止。@yueli很高兴提供帮助!我想知道可能存在readin文件问题。我正在努力解决它。@yueli您可以为此发布另一个问题,因为这将是不可能的主题。(社区指南)你好,ikegami,非常感谢你的帮助。它很有效!没问题。如果这回答了你的问题,请检查它旁边的标记。添加了一个缺少的
chomp
。你好,ikegami。我可以通读你的代码。我可以问你一个简单的问题吗?为什么拆分(/\t/,$\1)中有“-1”再次感谢您的帮助!非常适合添加chomp!谢谢!
1   2           2       5               4
4   4   4           4               3   
0       4   4           1               
0   1   5   6       4                   
use strict;
use warnings qw( all );
use feature qw( say );

while (<>) {
   chomp;
   my @fields = split(/\t/, $_, -1);
   for my $field (@fields) {
      $field = 0 if $field eq "";
   }

   say join "\t", @fields;
}
use strict;
use warnings;
my $filename = "c:\path\to\file.txt";
my $x;
    open(my $fh, '<', $filename) or die "cannot open file $filename: $!";
    {
        local $/;
        $x= <$fh>;
    }
    close($fh);


$x=~s/(\n )/\n0/g;      #starting zeros
$x=~s/( \n)/ 0\n/g;     #ending zeros
$x=~s/( $)/ 0\n/g;      #last zero if no end line on end of string
$x=~s/(^ )/0/g;         #first zero at beginning of string
$x=~s/(    )/   0/g;    #zeros within the matrix

print $x;