如何将哈希值传递给子进程?

发布于 2024-11-29 19:03:47 字数 1952 浏览 1 评论 0原文

在我的 Perl“脚本”中,我正在收集数据并构建哈希图。哈希映射键代表字段名称,值代表我要插入到相应字段中的值。

构建 hashmap,然后传递给 saveRecord() 方法,该方法应该构建 SQL 查询并最终执行它。

这里的想法是更新数据库一次,而不是每个字段一次(有很多字段)。

问题:我无法将哈希映射传递给子映射,然后将字段和值从哈希映射中提取出来。此时我的键和值都是空白的。我怀疑数据在传递给子进程的过程中丢失了。

脚本的输出表明没有键也没有值。

需要帮助将数据传递给子组件,以便我可以使用 join() 将其拉开,如图所示。

谢谢!

代码片段:

for my $key (keys %oids) {
        $thisField = $key;
        $thisOID = $oids{$thisField};
        # print "loop: thisoid=$thisOID field=$thisField\n";

        # perform the SNMP query.
        $result = getOID ($thisOID);
        # extract the information from the result.
        $thisResult = $result->{$thisOID};

        # remove quotation marks from the data value, replace them with question marks.
        $thisResult =~ s/\"|\'|/\?/g;

        # TODO: instead of printing this information, pass it to a subroutine which writes it to the database (use an update statement).
        # printf "The $thisField for host '%s' is '%s'.\n", $session->hostname(), $result->{$thisOID};

        # add this key/value pair to the mydata hashmap.
        $mydata{$thisField} = $thisResult;

        # print "$thisField=$thisResult\n";
}


# write one record update for hashmap %mydata.
saveRecord (%mydata);


# write all fields to database at once...
sub saveRecord ($) {
        my $refToFields=shift;


        my @fieldlist = keys %$refToFields;
        my @valuelist = values %$refToFields;
        my $sql = sprintf ("INSERT INTO mytable (%s) VALUES (%s)",join(",",@fieldlist), join(",",@valuelist) );

        # Get ID of record with this MAC, if available, so we can perform SQL update
        my $recid = getidbymac ($MAC);

        print "sql=$sql\n";
    # TODO: use an insert or an update based on whether recid was available...
        # TODO: ID available, update the record
        # TODO: ID not available, insert record let ID be auto assigned.
}

In my perl "script" I'm collecting data and building a hashmap. The hashmap keys represent field names, and the values represent the value I want to insert into the corresponding field.

The hashmap is built, and then passed to the saveRecord() method which is supposed to build a SQL query and eventually it will execute it.

The idea here is to update the database once, rather than once per field (there are a lot of fields).

The problem: I'm having trouble passing the hashmap over to the sub and then pulling the fields and values out of the hashmap. At this point my keys and values are blank. I suspect the data is getting lost during the passing to a sub.

The output of the script indicates no keys and no values.

Need help passing the data to the sub in a way that lets me pull it back apart as shown - with join().

Thanks!

Code snippet:

for my $key (keys %oids) {
        $thisField = $key;
        $thisOID = $oids{$thisField};
        # print "loop: thisoid=$thisOID field=$thisField\n";

        # perform the SNMP query.
        $result = getOID ($thisOID);
        # extract the information from the result.
        $thisResult = $result->{$thisOID};

        # remove quotation marks from the data value, replace them with question marks.
        $thisResult =~ s/\"|\'|/\?/g;

        # TODO: instead of printing this information, pass it to a subroutine which writes it to the database (use an update statement).
        # printf "The $thisField for host '%s' is '%s'.\n", $session->hostname(), $result->{$thisOID};

        # add this key/value pair to the mydata hashmap.
        $mydata{$thisField} = $thisResult;

        # print "$thisField=$thisResult\n";
}


# write one record update for hashmap %mydata.
saveRecord (%mydata);


# write all fields to database at once...
sub saveRecord ($) {
        my $refToFields=shift;


        my @fieldlist = keys %$refToFields;
        my @valuelist = values %$refToFields;
        my $sql = sprintf ("INSERT INTO mytable (%s) VALUES (%s)",join(",",@fieldlist), join(",",@valuelist) );

        # Get ID of record with this MAC, if available, so we can perform SQL update
        my $recid = getidbymac ($MAC);

        print "sql=$sql\n";
    # TODO: use an insert or an update based on whether recid was available...
        # TODO: ID available, update the record
        # TODO: ID not available, insert record let ID be auto assigned.
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑红尘 2024-12-06 19:03:47

我稍微清理了一下你的代码。您的主要问题是在调用您的子系统时没有使用参考。另请注意已清理的注释正则表达式:

代码:

use strict;
use warnings;

# $thisResult =~ s/["']+/?/g;
my %mydata = ( 'field1' => 12, 'field2' => 34, );

saveRecord (\%mydata); # <-- Note the added backslash

sub saveRecord {
    my $ref = shift;
    my $sql = sprintf "INSERT INTO mytable (%s) VALUES (%s)",
        join(',', keys %$ref),
        join(',', values %$ref);
    print "sql=$sql\n";
}

输出:

sql=INSERT INTO mytable (field1,field2) VALUES (12,34)

I cleaned up your code a little. Your main problem was not using a reference when calling your sub. Also note the commented regex which is cleaned up:

Code:

use strict;
use warnings;

# $thisResult =~ s/["']+/?/g;
my %mydata = ( 'field1' => 12, 'field2' => 34, );

saveRecord (\%mydata); # <-- Note the added backslash

sub saveRecord {
    my $ref = shift;
    my $sql = sprintf "INSERT INTO mytable (%s) VALUES (%s)",
        join(',', keys %$ref),
        join(',', values %$ref);
    print "sql=$sql\n";
}

Output:

sql=INSERT INTO mytable (field1,field2) VALUES (12,34)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文