Perl 从位置变量中去掉逗号

Perl 从位置变量中去掉逗号,perl,Perl,我有一个文件: 434462PW1 5 76252PPP8 5,714.79 76252PMB2 16,950.17 76252PRC5 25,079.70 76252PNY1 30,324.50 62630WCQ8 1.09 62630WCZ8 1.09 62630WBX4 36,731.90 62630WCQ8 1.07 62630WCZ8 1.07 76252PGB9

我有一个文件:

434462PW1       5
76252PPP8       5,714.79
76252PMB2       16,950.17
76252PRC5       25,079.70
76252PNY1       30,324.50
62630WCQ8       1.09
62630WCZ8       1.09
62630WBX4       36,731.90
62630WCQ8       1.07
62630WCZ8       1.07
76252PGB9       1.07
62630WBN6       1.07
62630WBA4       1.07
我需要去掉第二个值中的逗号,并在第一个值和第二个值之间添加一个逗号

434462PW1,5
76252PPP8,5714.79
76252PMB2,16950.17
76252PRC5,25079.70
76252PNY1,30324.50
62630WCQ8,1.09
62630WCZ8,1.09
62630WBX4,36731.90
62630WCQ8,1.07
62630WCZ8,1.07
76252PGB9,1.07
62630WBN6,1.07
62630WBA4,1.07
这是代码。我很难只剥离数值

#!/usr/bin/perl
use strict ; 
use warnings; 

open my $handle, '<', "foofile";
chomp(my @positionArray = <$handle>); 
foreach my $pos(@positionArray) {
        if ($pos =~ /(\w{9})\s+(.*)/) {
                if ($2=~/,/) {
                my $without = $2=~s/,//g   ;
        print "$1,$without\n";
        }
    }
}
#/usr/bin/perl
严格使用;
使用警告;

打开我的$handle,“因为逗号只出现在第2列中,所以只需删除每行中的所有逗号即可。此外,由于空格仅存在于两列之间,因此可以用逗号替换所有空格

foreach my $pos (@positionArray) {
    $pos =~ s/,//g;
    $pos =~ s/\s+/,/;
    print "$pos\n";
}

由于莫名其妙的原因,您使代码变得比实际情况更复杂

use strict ; 
use warnings;
use feature 'say';

my $filename = 'foofile';

open my $fh, '<', $filename
    or die "Couldn't open $filename $!";

my @lines = <$fh>;

close $fh;

chomp @lines;   # snip eol

for (@lines) {
    my($id,$val) = split;
    $val =~ s/,//;        # modifier 'g' might be used if value goes beyond thousands
    say "$id,$val";
}

另一种方法是,您可以使用
map
函数(输入和输出
@
数组变量)来解决此问题

chomp(my@positionArray=);
my@out=map{$\u=~ s/\,//g;$\u=~ s/\s+/,//;$\u;}@positionArray;
使用数据::转储程序;
打印转储文件\@输出;
434462PW1,5
76252PPP8,5714.79
76252PMB2,16950.17
76252PRC5,25079.70
76252PNY1,30324.50
62630WCQ8,1.09
62630WCZ8,1.09
62630WBX4,36731.90
62630WCQ8,1.07
62630WCZ8,1.07
76252PGB9,1.07
62630WBN6,1.07
62630WBA4,1.07
chomp(my @positionArray = <$handle>); 
my @out = map {  $_=~s/\,//g; $_=~s/\s+/,/; $_; }  @positionArray;
use Data::Dumper;
print Dumper \@out;