小编eou*_*uti的帖子

正确初始化空切片的方法

要声明具有非固定大小的空切片,最好是:

mySlice1 := make([]int, 0)
Run Code Online (Sandbox Code Playgroud)

要么:

mySlice2 := []int{}
Run Code Online (Sandbox Code Playgroud)

只是想知道哪一个是正确的方法.

arrays go slice

184
推荐指数
4
解决办法
17万
查看次数

Regex C++:提取子字符串

我想在另外两个之间提取一个子串.
前:/home/toto/FILE_mysymbol_EVENT.DAT
或者只是FILE_othersymbol_EVENT.DAT
我希望得到:mysymbolothersymbol

我不想使用boost或其他库.只是来自C++的标准内容,除了CERN的ROOT lib,还有TRegexp,但我不知道如何使用它...

c++ regex

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

Perl:常量和要求

我有一个配置文件(config.pl)与我的常量:

#!/usr/bin/perl
use strict;
use warnings;
use Net::Domain qw(hostname hostfqdn hostdomain domainname);

use constant URL => "http://".domainname()."/";
use constant CGIBIN => URL."cgi-bin/";
use constant CSS => URL."html/css/";
use constant RESSOURCES => URL."html/ressources/";
...
Run Code Online (Sandbox Code Playgroud)

我想在index.pl中使用这些常量,所以index.pl以:

#!/usr/bin/perl -w
use strict;
use CGI;
require "config.pl";
Run Code Online (Sandbox Code Playgroud)

如何在index.pl中使用URL,CGI ...
谢谢,
再见


编辑
我找到了一个解决方案:
config.pm

#!/usr/bin/perl
package Config;
use strict;
use warnings;
use Net::Domain qw(hostname hostfqdn hostdomain domainname);

use constant URL => "http://".domainname()."/";
use constant CGIBIN => URL."cgi-bin/";
1;
Run Code Online (Sandbox Code Playgroud)

index.pl

BEGIN {
    require "config.pm";
}
print …
Run Code Online (Sandbox Code Playgroud)

perl constants require include

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

Perl,perl构造函数中的@array

我写了perl类,但我不知道如何在我的$this变量中有一个数组或一个哈希?

我有一个pack.pm:

#!/usr/bin/perl -w
use strict;
use Parallel::ForkManager;
package Pack;

our $cgi = new CGI;

sub new {
    my ($classe, $nom, $nbports, $gio) = @_;

    my $this = {
    "nom"    => $nom,
    "nbports" => $nbports,
    "gio"   => $gio
    };

    bless($this, $classe);
    return $this;   
}
    ...
1;
Run Code Online (Sandbox Code Playgroud)

我想有一个@tab,我可以通过访问$this->tab,但我不想在arg中给它实例.
它在Perl中如何工作?

谢谢.

variables perl constructor instance

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

CoffeeScript - 使用参数执行bash脚本

我正在玩GitHub的Hubot,我尝试在我的机器人工作中执行一个bash脚本.
我成功执行了我的脚本,但如果我在这个脚本中添加一些参数,则无法使其正常工作.

{ spawn } = require 'child_process'
s = spawn './myScript.sh' + " url" + " title"     <------- doesn't work due to args
s = spawn './myScript.sh'                         <------- alright without args
s.stdout.on 'data', ( data ) -> console.log "Output: #{ data }"
s.stderr.on 'data', ( data ) -> console.error "Error: #{ data }"
s.on 'close', -> console.log "'s' has finished executing."
Run Code Online (Sandbox Code Playgroud)

如何将参数传递给我的脚本?
感谢帮助

javascript bash coffeescript hubot

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

Perl:创建已知大小的数组


我必须存储使用Parallel :: ForkManager运行的请求中的数据.
但它不按顺序,我的意思是,我轮询了一个交换机的所有端口,但其中一些回答比其他端口更快.所以,我不知道如何保存它,以后再显示它.

我能这样做吗?

my @array = ();
$array[10] = "i am a string corresponding to port 10"
$array[2] = "the one for the port 2"
...
print @array;
Run Code Online (Sandbox Code Playgroud)

或者我应该使用带有端口数的%hash作为键,但它似乎不是最好的.
谢谢.

arrays variables perl

4
推荐指数
3
解决办法
6187
查看次数

C++:fork/exec还是pthread?

我正在编写一个程序,一旦按下按钮,我就必须执行一个服务器进程(只有在我决定杀死他时才会停止).
为了执行这个过程,我决定使用fork/execv机制:

void Command::RunServer() {

    pid = fork();

    if (pid==0) {
        chdir("./bin");
        char str[10];
        sprintf(str,"%d",port);
        char *argv[] = {"./Server", str};
        execv("./Server",argv);
    }
    else {
        config->pid = pid;
        return;
    }
}
Run Code Online (Sandbox Code Playgroud)

在方法"按下按钮",我做:

command->RunServer();
Run Code Online (Sandbox Code Playgroud)

它似乎在几天前工作得很好......现在我得到错误:

main: xcb_io.c:221: poll_for_event: Assertion `(((long) (event_sequence) - (long) (dpy->request)) <= 0)' failed.
Run Code Online (Sandbox Code Playgroud)

我应该尝试切换到pthread吗?我做了坏事吗?

谢谢,
eo

c++ fork pthreads exec

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

结合综合与战略

我正在寻找一个示例,展示如何组合这两种设计模式(策略和复合).我知道如何使用策略,但复合材料对我来说还不够清楚,所以我无法真正看到如何将它们结合起来.有人有例子还是smthg?
干杯

design-patterns

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

Go - get父结构

我想知道如何检索实例的父结构.
我不知道如何实现这一点.

例如:

type Hood struct {
    name string
    houses  []House
}

type House struct {
    name   string
    people int16
}

func (h *Hood) addHouse(house House) []House {
    h.houses = append(h.houses, house)
    return h.houses
}

func (house *House) GetHood() Hood {
    //Get hood where the house is situated
    return ...?
}
Run Code Online (Sandbox Code Playgroud)

干杯

structure go

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

CGI.pm:包括图片?

我正在使用CGI.pm模块编写Perl/CGI脚本.我可以返回text/html,但不能在其中包含图像,它总是显示'alt'标签,我确定href链接.

这是我的代码:

#!/usr/bin/perl -w
use strict;
use CGI;

my $cgi = new CGI;

print $cgi->header(-type=>'text/html'),
      $cgi->start_html(-title=>'Test Page'),
      $cgi->h1('Infos switch'),
        "This is a test.",
      $cgi->h1('Testing'), $cgi->hr, "\n",
      $cgi->img(-src=>'http://www.perl.org/simages/lcamel.gif',  
                -alt=>'Powered by Perl'), "\n\n",
      $cgi->end_html;
exit;
Run Code Online (Sandbox Code Playgroud)

perl cgi

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

为什么我使用Text :: CSV收到错误消息»无法解析行«?

我正在尝试解析Perl中的CSV文件,但是我并不真正理解我在Internet上找到的示例.有人能解释一下这个例子吗?

#!/usr/bin/perl
use strict;
use warnings;
use Text::CSV;

my $file = 'dhcp.csv';

my $csv = Text::CSV->new();

open (CSV, "<", $file) or die $!;

while (<CSV>) {
    next if ($. == 1);
    if ($csv->parse($_)) {
        my @columns = $csv->fields();
        print "Name: $columns[0]\n\tContact: $columns[4]\n";
    } else {
        my $err = $csv->error_input;
        print "Failed to parse line: $err";
    }
}
close CSV;
Run Code Online (Sandbox Code Playgroud)

当我跑它,我得到Failed to parse line.这$.代表什么?而且$_

我的目标是找到我搜索的计算机名称所在的行.之后,我可以找到相应的MAC地址.我希望这是可以理解的,谢谢.


编辑:

我的CSV文件如下:

172.30.72.22,DEC-16.rec.local,001676b755d6,Bart SIMPSONS,Ordinateur de bureau,DEC/DECVA,002,SR2 0.12,,Accès complet,N/D,Aucun
172.30.72.23,DEC-20.rec.local,001688b7bfdc,Larry …
Run Code Online (Sandbox Code Playgroud)

csv perl

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

Perl/Net :: SNMP:脚本太慢,需要优化


我想优化我的perl脚本,因为它显示有关网络的信息有点慢.
我不知道可以改变或改进什么来促进脚本执行.
我操纵几个哈希,得到:mac add,index等...我认为它有点沉重,但别无选择.
而且,我做了很多SNMP请求,错误处理可能不是很好.
我复制/粘贴我的脚本及其模块.
提前感谢您阅读我的代码.

它需要args:

  1. 接口名称(例如FastEthernet0/9或FastEthernet0/1 ......)
  2. hostname:交换机的ip
  3. 社区(通常=公共)

希望这是可以理解的.

 #!/usr/bin/perl
    use strict;
    use warnings;
    use Getopt::Long;
    use SnmpUtil;
    use AdresseMac;
    use Net::SNMP;
    use Net::SNMP::Interfaces;

    my $ifname;
    my $hostname;
    my $community;
    my $version = 1;

    GetOptions( "ifname=s"  => \$ifname,
                "host=s"    => \$hostname,
                "community=s"   => \$community,
                "protocol:s"    => \$version);

    my $interfaces = Net::SNMP::Interfaces->new(Hostname => $hostname, Community => $community);
    my $inter = $interfaces->interface($ifname);
    #Get interface $ifname
    my $ifindex = $inter->index();
    #Vitesse
    my $vitesse = $inter->ifHighSpeed();
    #Alias
    my $ifalias = …
Run Code Online (Sandbox Code Playgroud)

optimization perl snmp

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

优化Perl脚本


是否有可能使这个脚本更快?

#!/usr/bin/perl -w
    use strict;
    use CGI;
    package SwitchGUI;

    sub new {
        my ($classe, $nom, $nbports, $gio) = @_;

        my $this = {
            "nom"     => $nom,
            "nbports" => $nbports,
            "gio"     => $gio
        };

        bless($this, $classe);
        $this->afficher();
        return $this;   
    }

    sub afficher {
        my ($this) = @_;
        my @tab = ( 1 .. $this->{nbports} );
        my @odd = grep { $_ % 2 } @tab;
        my @even = grep { not $_ % 2 } @tab;

        my $cgi = new CGI; …
Run Code Online (Sandbox Code Playgroud)

optimization perl cgi

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