小编Tak*_*kun的帖子

如何在多个类之间传递对象?Java的

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)

java reference

2
推荐指数
1
解决办法
1万
查看次数

在Python中通过HTTP请求发送XML的最简单方法?

是否有一个首选的python模块可以帮助我通过HTTP请求发送XML并能够解析返回的XML?

python xml httprequest

2
推荐指数
1
解决办法
2万
查看次数

在Perl中尝试/捕获键盘中断?

如何在Perl中捕获键盘中断?

while(1) {
    try {
        print 1;
    } 
    catch KeyboardInterrupt {
        print 2;
    }
}
Run Code Online (Sandbox Code Playgroud)

perl signals

2
推荐指数
1
解决办法
635
查看次数

Dijkstra的算法是线性时间吗?

O(|E| + |V| log |V|)
Run Code Online (Sandbox Code Playgroud)

我知道愚蠢的问题,但如果有一个日志是线性的吗?

algorithm a-star dijkstra

2
推荐指数
1
解决办法
1832
查看次数

为什么这些字符串不相等?(蟒蛇)

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来构造字典.

python

1
推荐指数
2
解决办法
259
查看次数

如何将共享数据传递给Perl中的线程?

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)

为什么不在线程中共享数据?

perl multithreading

1
推荐指数
1
解决办法
843
查看次数

在循环中交替变量值的最佳方法?(Perl的)

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循环的每次迭代,在两个不同值之间交替的最佳方法是什么?上面的简单例子,是否有比保持计数器和修改更好的方法来查找偶数值?

perl loops

1
推荐指数
1
解决办法
1006
查看次数

Perl - Roundrobin通过数组的哈希

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)

等等...

perl loops round-robin

1
推荐指数
1
解决办法
270
查看次数

如何根据整数中的数字更改空格数?

我想在每行开头用行号打印出数组中的值.我想更改间距以确保值垂直排列,而不管数组的大小.

@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)

这有效,但我想知道是否有更好的方法来做到这一点?

ruby

1
推荐指数
1
解决办法
50
查看次数

使用正则表达式获取由不同数量的空格分隔的单词?

我试图抓住字符串中的每个单词,可以用不同数量的空格分隔.

my $ff = "Disk  DSM     Policy  Paths   Serials";

if($ff =~ m/(\w+)/) {
    print $1;
    print $2;
    print $3;
}
Run Code Online (Sandbox Code Playgroud)

我认为它会像\ w一样简单,但它只能得到第一个字?难道我做错了什么?

regex perl

0
推荐指数
1
解决办法
894
查看次数

根据定义分配两个可能值中的变量之一的最佳方法?

    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中有一种单行方式吗?或者至少比我在那里更简单?

perl

0
推荐指数
2
解决办法
93
查看次数

你可以使用grep将字符串与"模板"字符串进行比较吗?

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)

regex perl

0
推荐指数
1
解决办法
73
查看次数