在Perl中,我可以看到类似的警告
Use of uninitialized value in concatenation (.) or string at stat_plots.pl line 446.
Run Code Online (Sandbox Code Playgroud)
但是,我想对perl进行更改,或者在脚本的开头添加一个子例程,以便此警告会显示错误,如
Hash key {index} is not defined at stat_plots.pl line 446.
Run Code Online (Sandbox Code Playgroud)
这将节省调试过程中的大量时间,并使代码看起来更好,而无需添加数十个
if (!defined $hash{$key}) {
print "\$hash{$key} isn't defined.\n";
}
Run Code Online (Sandbox Code Playgroud)
并使每个脚本更长,更麻烦.
有没有办法让perl这样做?
我有一个来自CSV文件的字符串:
my $str = 'NA19900,4,111629038,0;0,0;0,"GSA-rs16997168,rs16997168,rs2w34r23424",C,T,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0';
Run Code Online (Sandbox Code Playgroud)
应该翻译(以某种方式)为
'NA19900,4,111629038,0;0,0;0,"GSA-rs16997168;rs16997168;rs2w34r23424",C,T,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0';
Run Code Online (Sandbox Code Playgroud)
这样perl split
不会将单个字段GSA-rs16997168,rs16997168
分成两个单独的字段
也就是说,如果逗号介于两者之间,则应将逗号替换为分号,但"
我在Google上找不到该方法
到目前为止,我已经尝试过:
$str =~ s/"([^"]+),([^"]+)"/"$1;$2"/g;
但这失败了> 2个表达式
如果我能以某种方式告诉perl split
函数将""
一个字段中的所有内容都计为一个字段,那将是很棒的,即使该文本具有,
分隔符,但我也不知道该怎么做:(
我听说过前瞻,但是在这里我看不到如何使用它们:(
我正在尝试安装 Perl 包Vcf
,它有很多依赖项。这些依赖项之一DB_File
将不会安装。
con@VB:~$ cpanm DB_File
--> Working on DB_File
Fetching http://www.cpan.org/authors/id/P/PM/PMQS/DB_File-1.852.tar.gz ... OK
Configuring DB_File-1.852 ... OK
Building and testing DB_File-1.852 ... FAIL
! Installing DB_File failed. See /home/con/.cpanm/work/1567015698.27372/build.log for details. Retry with --force to force install it.
con@VB:~$ cat /home/con/.cpanm/work/1567015698.27372/build.log
cpanm (App::cpanminus) 1.7044 on perl 5.030000 built for x86_64-linux
Work directory is /home/con/.cpanm/work/1567015698.27372
You have make /usr/bin/make
You have LWP 6.39
You have /bin/tar: tar (GNU tar) 1.29
Copyright (C) 2015 Free Software Foundation, …
Run Code Online (Sandbox Code Playgroud) 我正在尝试学习Ruby,并且想知道如何将一个数组用于索引另一个数组,例如,
在Perl中,这是:my @x = @y[@ro]
,其中所有三个变量都是通用数组。
如何在Ruby中完成同一件事?
我有一个可以在 Perl < 5.36 上运行良好的脚本:
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use feature 'say';
use autodie ':all';
use Parallel::ForkManager;
sub execute {
my $command = shift;
print "Executing Command: $command\n";
if (system($command) != 0) {
my $fail_filename = "$0.fail";
print "$command failed.\n";
die;
}
}
sub run_parallel {
my $cmd = shift;
my $manager = new Parallel::ForkManager(2);
foreach my $command (@{ $cmd }) {
$manager->start and next;
execute( $command );
$manager->finish;
}
$manager->wait_all_children;#necessary after all lists
} …
Run Code Online (Sandbox Code Playgroud) 我试图计算文本文件的第3列中唯一出现的数字,这是一个非常简单的命令:
awk 'BEGIN {FS = "\t"}; {print $3}' bisulfite_seq_set0_v_set1.tsv | uniq -c
Run Code Online (Sandbox Code Playgroud)
应该说类似的东西
1 10103
2 2093
3 109
Run Code Online (Sandbox Code Playgroud)
但是反而出现废话,其中相同的数字被多次计算,例如
20 1
1 2
1 1
1 2
14 1
1 2
Run Code Online (Sandbox Code Playgroud)
我也试过了
awk 'BEGIN {FS = "\t"}; {print $3}' bisulfite_seq_set0_v_set1.tsv | sed -e 's/ //g' -e 's/\t//g' | uniq -c
Run Code Online (Sandbox Code Playgroud)
我尝试过uniq手册页中我能想到的所有组合.如何使用uniq正确计算数字的唯一出现次数?
我有一个程序,其计算的概率值(p -值),但它正在进入一个非常大的负数到
exp
函数
exp(-626294.830)
,其计算结果为零,而不是非常小的正数,它应该是.
我怎样才能将其评估为一个非常小的浮点数?我曾尝试
Math::BigFloat
,
bignum
和
bigrat
,但都失败了.
我正在尝试按照https://metacpan.org/pod/IPC::Run中的描述运行IPC :: Run
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use feature 'say';
use autodie qw(:all);
use IPC::Run qw(run timeout);
my ($out, $err);
## First,a command to run:
my @cat = qw( cat );
## Using run() instead of system():
my $in = __FILE__;
run \@cat, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?";
say "\$out = $out";
say "\$err = $err";
say "\$in = $in";
Run Code Online (Sandbox Code Playgroud)
但是,输出不正确:
con@con-VirtualBox:~/Scripts$ perl ipc_run.pl
$out = ipc_run.pl
$err …
Run Code Online (Sandbox Code Playgroud) 我正在使用Perl 5.26.1读取CSV文件,其行如下所示:
B1_10,202337840166,R08C02,202337840166_R08C02.gtc
我正在将这些数据读取到一个哈希中,该哈希的最后一个元素为键,第一个元素为值。
我逐行读取文件(仅片段):
while (<$csv>) {
if (/^Sample/) { next }
say "-----start----\noriginal = $_";
chomp;
my @line = split /,/;
my $name = $line[0];
my $vcf = $line[3];
say "1st element = $name";
say "4th element = $vcf";
$vcf2dir{$vcf} = $name;
say "\$vcf2dir{$vcf} = '$name'";
say '-----end------';
}
Run Code Online (Sandbox Code Playgroud)
产生以下输出:
-----start----
original = B1_10,202337840166,R08C02,202337840166_R08C02.gtc
1st element = B1_10
4th element = 202337840166_R08C02.gtc
} = 'B1_10'2337840166_R08C02.gtc
-----end-------
Run Code Online (Sandbox Code Playgroud)
但它看起来应该像
-----start----
original = B1_10,202337840166,R08C02,202337840166_R08C02.gtc
1st element = B1_10
4th element = …
Run Code Online (Sandbox Code Playgroud) 我是 C++ 的新手。我正在尝试学习字符串替换。
我正在编写一个简短的(应该是教程)程序来更改目录名称。
例如,我想将目录名称开头的“/home”更改为“/illumina/runs”,因此:
#ifdef WINDOWS
#include <direct.h>
#define GetCurrentDir _getcwd
#else
#include <unistd.h>
#define GetCurrentDir getcwd
#endif
#include<iostream>
#include <string>
#include <regex>
#include <iterator>
using namespace std;
std::string get_current_dir() {
char buff[FILENAME_MAX]; //create string buffer to hold path
GetCurrentDir( buff, FILENAME_MAX );
string current_working_dir(buff);
return current_working_dir;
}
int main() {
string b2_dir = get_current_dir();
std::regex_replace(b2_dir, std::regex("^/home"), "/illumina/runs");
cout << b2_dir << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我正在遵循http://www.cplusplus.com/reference/regex/regex_replace/ 中的一个示例,但我不明白为什么这没有改变。
万一我写的没有意义,Perl 等价物$dir =~ s/^\/home/\/illumina\/runs/
是否有助于更好地理解问题
为什么这段代码不像我告诉它的那样改变字符串?我怎样才能让 C++ 改变字符串?
我有一个未定义值的数组:
[0] 0.1431,
[1] undef
Run Code Online (Sandbox Code Playgroud)
然后我把这个值推到一个数组上,Perl 没有警告我未初始化的值(我无法想象为什么有人会希望在没有警告的情况下发生这种情况,或者die
,即使我已经use autodie ':all'
设置,但那是另一回事了)
所以我尝试grep
在这些数组上防止将undef
值推送到数组上,因此:
if (grep {undef} @array) {
next
}
Run Code Online (Sandbox Code Playgroud)
但是,这并没有抓住价值。
如何grep
在 Perl5 中获取未定义的值?
我正在运行一个简单的脚本,如果定义了一个值,它将为一个变量分配一个值,最简单的是:
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use feature 'say';
use autodie ':default';
my $x;
my $y = $x || 3;
say $y; # prints 3, as it should, because $x isn't defined
$x = 9;
$y = $x || 3;
say $y; # prints 9, as it should, because $x is defined
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试在子例程中使用相同的语法时,它会反向工作:
sub subr {
my ($args) = @_;
my $min = $args->{min} || 4; # returns 4, not $args->{min}
say $min;
}
subr({
min …
Run Code Online (Sandbox Code Playgroud)