Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.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函数(sub),名为;逗号;将字符串作为输入,将字符串拆分为数组_Perl - Fatal编程技术网

我想写一个perl函数(sub),名为;逗号;将字符串作为输入,将字符串拆分为数组

我想写一个perl函数(sub),名为;逗号;将字符串作为输入,将字符串拆分为数组,perl,Perl,我想编写一个名为“逗号”的perl函数(sub),它以字符串作为 输入时,根据逗号将字符串拆分为数组(忽略 逗号周围的空格),并返回字符串数组。这个 catch是任何元素都可以包含在引号中,其中逗号 将被允许 输入: 输出: 输入: 输出: 我的实现代码 文本::CSV是一个更好的主意!无论如何,我喜欢写Perl #!/usr/bin/perl sub comma{ my $s=shift; ## poor man's CSV my

我想编写一个名为“逗号”的perl函数(sub),它以字符串作为 输入时,根据逗号将字符串拆分为数组(忽略 逗号周围的空格),并返回字符串数组。这个 catch是任何元素都可以包含在引号中,其中逗号 将被允许

输入: 输出: 输入: 输出: 我的实现代码
文本::CSV是一个更好的主意!无论如何,我喜欢写Perl

#!/usr/bin/perl

sub comma{ my $s=shift;                              ## poor man's CSV
  my %save=();
  my $i=0;
  $s =~ s/(".*?")/$save{++$i}=$1; "--$i--"/ge;       ## save strings
  map {s/--(\d+)--/$save{$1}/r} split(/\s*,\s*/,$s); ## split and restore
}

while(<DATA>){                                       ## testing
  print join("\n---",comma($_)),"\n";
}

__DATA__
one,two,"three,three and a half",  four    ,"five, five and a half",six
#/usr/bin/perl
次逗号{my$s=shift;##穷人的CSV
我的%save=();
我的$i=0;
$s=~s/(“*?”)/$save{++$i}=$1;“--$i--”/ge;##保存字符串
映射{s/-(\d+)-/$save{$1}/r}拆分(/\s*,\s*/,$s);##拆分和还原
}
while(){###测试
打印联接(“\n--”,逗号($),“\n”;
}
__资料__
一,二,“三,三点半”,四,“五,五点半”,六

这个问题是离题的,因为它是关于编程的,而不是关于Unix和Linux。。。但您描述的格式听起来像CSV。您是否考虑过使用库将其解析为CSV?关键是任何元素都可能包含在引号中——不要为此而使用拆分或单个regexp,您不能这样做。你需要一个状态机(或者只使用模块)。使用Text::CSV并避免重新发明wheels听起来像是你想要的
parse_line
函数(它一直是标准Perl发行版的一部分)。它为你编译了吗?当你用子逗号传递数据时?@sam代码中有一个输入错误(一个剪切粘贴的奇怪的“k”),你能再检查一下吗?(
perl文件
)像这样的字符串怎么样:
--1-,--2-
?@JESUISCHARLIE,好吧,好吧,我试图避免混淆代码。通常我使用非文本标记(
$s=~s/(“*?”)/$save{++$I}=$1;“\cA$I\cA”/ge;
)--你当然是对的。像
一、二、“三加一”半、“四”这样的字符串呢?
hello   
"world, yo"    
math
one,two,"three,three and a half",          four              ,"five, five and a half",six
one    
two    
"three,three and a half"    
four    
"five, five and a half"     
six     
my $testb = "hello,'world, yo',matt";
print $_, "\n" for split ',', $testb;

my $testc = "one,two,"three,three and a half",          four              ,"five, five and a half",six";
print $_, "\n" for split ',', $testc;
#!/usr/bin/perl

sub comma{ my $s=shift;                              ## poor man's CSV
  my %save=();
  my $i=0;
  $s =~ s/(".*?")/$save{++$i}=$1; "--$i--"/ge;       ## save strings
  map {s/--(\d+)--/$save{$1}/r} split(/\s*,\s*/,$s); ## split and restore
}

while(<DATA>){                                       ## testing
  print join("\n---",comma($_)),"\n";
}

__DATA__
one,two,"three,three and a half",  four    ,"five, five and a half",six