我正在阅读Perl中的文件,我想根据两个分隔符拆分值.
col1 col2
uc011nbb.2 NM_001039567
uc004fuo.4 NM_001006120
uc011nbc.2 NM_001006121
uc010nwz.3 NM_001006121
Run Code Online (Sandbox Code Playgroud)
col 1和col2由制表符分隔,所以我通常使用这个:
my @cols = split(/\t/);
Run Code Online (Sandbox Code Playgroud)
但是,我想将col1也拆分为'.'.如何修改我的分割功能呢?
您可以使用a character class来编码两种可能性:
split /[.\t]/;
Run Code Online (Sandbox Code Playgroud)
正如上面提到的@Sobrique所述,.在将其用作分隔符之前,应确保不会出现在任何列名中.
例:
say foreach split /[.\t]/, "this is some.text that has\ttwo delimiters";
this is some
text that has
two delimiters
Run Code Online (Sandbox Code Playgroud)