public class FooClass {
BarClass bar = null;
int a = 0;
int b = 1;
int c = 2;
public FooClass(BarClass bar) {
this.bar = bar;
bar.setFoo(this);
}
}
public class BarClass {
FooClass foo = null;
public BarClass(){}
public void setFoo(FooClass foo) {
this.foo = foo;
}
}
Run Code Online (Sandbox Code Playgroud)
别处...
BarClass theBar = new BarClass();
FooClass theFoo = new FooClass(theBar);
theFoo.a //should be 0
theBar.foo.a = 234; //I change the variable through theBar. Imagine all the variables are private …Run Code Online (Sandbox Code Playgroud) 是否有一个首选的python模块可以帮助我通过HTTP请求发送XML并能够解析返回的XML?
如何在Perl中捕获键盘中断?
while(1) {
try {
print 1;
}
catch KeyboardInterrupt {
print 2;
}
}
Run Code Online (Sandbox Code Playgroud) def getValue(d, key):
for k, v in d.iteritems():
print "{0} == {1}".format(k, key)
if k == key:
return v
elif isinstance(v, dict):
getValue(v, key)
logging.error("Cannot find key in dictionary")
return ""
#d = getting the dictionary
getValue(d, "error_frames")
Run Code Online (Sandbox Code Playgroud)
从我在函数中插入的print语句中,我清楚地看到控制台中出现"error_frames == error_frames",但if语句没有被执行.为什么?通过使用模块xmltodict解析xml来构造字典.
use threads;
use threads::shared;
sub test {
my $s :shared = 22;
my $thread = threads->new(\&thrsub);
$thread->join();
print $s;
}
sub thrsub {
$s = 33;
}
test;
Run Code Online (Sandbox Code Playgroud)
为什么不在线程中共享数据?
my @arr = (1,2,3,4,5,6,7,8,9);
my $counter = 0;
foreach my $a (@arr) {
my $str;
if ($counter % 2 == 0) {
$str = 'hi';
} else {
$str = 'bye';
}
print $str . "\n";
$counter++;
}
Run Code Online (Sandbox Code Playgroud)
对于while循环的每次迭代,在两个不同值之间交替的最佳方法是什么?上面的简单例子,是否有比保持计数器和修改更好的方法来查找偶数值?
my %aggrs_by_node = (
node1 => ['a1_1','a1_2'],
node2 => ['a2_1','a2_2','a2_3'],
hello => ['ah_1','ah_2','ah_3'],
node3 => ['a3_1','a3_2','a3_3','a3_4'],
);
Run Code Online (Sandbox Code Playgroud)
任何人都有任何关于我如何循环遍历此哈希中的所有数组元素同时避免密钥的建议hello?
Iteration 1 = a1_1
Iteration 2 = a2_1
Iteration 3 = a3_1
Iteration 4 = a1_2
Iteration 5 = a2_2
Iteration 6 = a3_2
Iteration 7 = a1_1
Iteration 8 = a2_3
Run Code Online (Sandbox Code Playgroud)
等等...
我想在每行开头用行号打印出数组中的值.我想更改间距以确保值垂直排列,而不管数组的大小.
@lines.each_with_index {|l,i|
newtext = l[:text]
space = ' ' * @lines.size.to_s.size
cur_space = i.to_s.size
(1..cur_space).each { space.chop! }
puts "\##{i}#{space} #{newtext}"
}
Run Code Online (Sandbox Code Playgroud)
产量
#0 blah
#1 blah
#2 blah
#3 blah
#4 blah
#5 blah
#6 blah
#7 blah
#8 blah
#9 blah
#10 blah
#11 blah
#12 blah
#13 blah
Run Code Online (Sandbox Code Playgroud)
这有效,但我想知道是否有更好的方法来做到这一点?
我试图抓住字符串中的每个单词,可以用不同数量的空格分隔.
my $ff = "Disk DSM Policy Paths Serials";
if($ff =~ m/(\w+)/) {
print $1;
print $2;
print $3;
}
Run Code Online (Sandbox Code Playgroud)
我认为它会像\ w一样简单,但它只能得到第一个字?难道我做错了什么?
my $port_string;
if (defined($self->{port_range})) {
$port_string = $self->{port_range};
} else {
$port_string = "whatever2";
}
Run Code Online (Sandbox Code Playgroud)
如果已定义,我想分配一个变量值,如果第一个值是undef,我想分配另一个值.
在Perl中有一种单行方式吗?或者至少比我在那里更简单?
my @array = (
'There were \d* errors that occurred',
'Your system exploded because \.*',
);
my $error = 'There were 22 errors that occurred';
if (grep(/$error/, @array)) {
print 'That error is ok, continue...';
} else {
die;
}
Run Code Online (Sandbox Code Playgroud)
在perl中是否有任何方法可以将完整字符串与包含正则表达式的字符串进行比较?就像在这个例子中我想要两个$ error ='发生了22个错误'和$ error ='发生了12341235个错误'与一种"模板"字符串进行比较并返回一个布尔值火柴.我想,使用grep可能是不可能的.
也许像这样的东西实际上有效:
my @s = ('there were \d* errors');
print _error_checker(@s, 'there were 10 errors');
sub _error_checker {
my (@acceptable_errors, $text) = @_;
foreach my $error (@acceptable_errors) {
if ($text =~ /$error/) {
return 1;
} …Run Code Online (Sandbox Code Playgroud)