我试图迭代使用嵌套哈希的数据结构中的项目.为此,我想看看那里有什么键.
以下是我的尝试.但是我收到了一个错误
my %tgs = (
'articles' => {
'vim' => 'about vim',
'awk' => 'about awk',
'sed' => 'about sed'
},
'ebooks' => {
'linux 101' => 'about linux',
}
);
foreach my $k (keys %tgs){
print $k;
print "\n";
foreach my $k2 (keys %$tgs{$k}){ #<-----this is where perl is having a problem
print $k2;
print "\n";
}
}
syntax error at PATH line #, near "$tgs{"
syntax error at PATH line #, near "}"
Execution of PATH aborted …Run Code Online (Sandbox Code Playgroud) 为什么构造函数中的assert语句在以下代码中不起作用?(我想用它而不是if语句,我注释掉了)
import java.util.HashMap;
import java.util.Map;
enum Suite{
HEARTS, DIAMONDS, SPADES,CLUBS;
}
public class Card {
private Suite suite;
private int number;
private String name;
public Card(int number, Suite suite) {
// if(number < 0 || number > 13)
// throw new IllegalArgumentException();
assert(!(number < 0 || number >13));
this.number=number;
this.suite=suite;
initialize();
}
private void initialize(){
switch(number){
case 1: name="Ace"; break;
case 2: name="Two"; break;
case 3: name="Three"; break;
case 4: name="Four"; break;
case 5: name="Five"; break;
case 6: name="Six"; break; …Run Code Online (Sandbox Code Playgroud) 我想替换
hi@my@name@is@foo
Run Code Online (Sandbox Code Playgroud)
成
hi@my@name@is
foo
Run Code Online (Sandbox Code Playgroud)
这是用"\n"字符替换最后一个@.这是我的尝试性解决方案,但我觉得这可以做得更好.
echo "hi@my@name@is@foo"| perl -pe '$_ = reverse($_);s/@/\n/;$_=reverse($_)'
Run Code Online (Sandbox Code Playgroud) 我试图在多类预测问题上拟合xgboost模型,并希望caret用来进行超参数搜索.为了测试包,我使用了以下代码,当我不train使用trainControl 提供对象时需要20秒
# just use one parameter combination
xgb_grid_1 <- expand.grid(
nrounds = 1,
eta = 0.3,
max_depth = 5,
gamma = 0,
colsample_bytree=1,
min_child_weight=1
)
# train
xgb_train_1 = train(
x = as.matrix(sparse_train),
y = conversion_tbl$y_train_c ,
trControl = trainControl(method="none", classProbs = TRUE, summaryFunction = multiClassSummary),
metric="logLoss",
tuneGrid = xgb_grid_1,
method = "xgbTree"
)
Run Code Online (Sandbox Code Playgroud)
但是,当我提供traintrainControl对象时,代码永远不会完成..或者花费很长时间(至少它完成了15分钟.
xgb_trcontrol_1 <- trainControl(
method = "cv",
number = 2,
verboseIter = TRUE,
returnData = FALSE,
returnResamp = "none", …Run Code Online (Sandbox Code Playgroud) 假设我有以下文字.
a test1
b test2
a test3
b test4
Run Code Online (Sandbox Code Playgroud)
什么是用于提取以字母a开头的行并将它们放在文件末尾的命令是什么?
b test2
b test4
a test1
a test3
Run Code Online (Sandbox Code Playgroud)
当我使用:g/^ a/d和p时,粘贴唯一的最后一个匹配:
b test2
b test4
a test3
Run Code Online (Sandbox Code Playgroud) 我有一个时间戳数据具有以下结构,其中日期和时间以下列方式显示:
timestamp
January 22,2013 20:56
January 22,2013 08:53
January 22,2013 20:59
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能把它变成R中的时间对象?我打算编写一个perl脚本将数据转换为YEAR-MONTH-TIME HH:MM:SS格式,并使用POSIXct将其导入日期时间对象,但我想知道是否有一个快捷方式可以执行此操作R.
我能够在我的本地机器上运行以下代码,我有一个sudo的力量"
#!/usr/bin/env perl
package Cat
{
use Moose;
has 'name', is => 'ro', isa => 'Str';
}
my $test_obj = Cat->new(name => "kitty");
print $test_obj->name()."\n";
Run Code Online (Sandbox Code Playgroud)
结果:
$perl Cat.pl
kitty
Run Code Online (Sandbox Code Playgroud)
但是当我在其他没有sudo的机器上运行完全相同的代码时,我收到以下错误:
syntax error at Cat.pl line 5, near "{
"
Execution of Cat.pl aborted due to compilation errors.
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
我在我的三台机器上使用cpanm安装了Module,一台使用sudo,两台没有sudo(我在本地安装了它们).perl的版本是:
machine 1(worked, has sudo) : (v5.14.2) built for cygwin-thread-multi-64int
machine 2(did not work, no sudo previlage) : perl, v5.10.1 (*) built for x86_64-linux-thread-multi
machine 3(did not work, no sudo previlage) : …Run Code Online (Sandbox Code Playgroud)