Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 使用整数和字符串的多维数组_Perl_Arrays_Multidimensional Array - Fatal编程技术网

Perl 使用整数和字符串的多维数组

Perl 使用整数和字符串的多维数组,perl,arrays,multidimensional-array,Perl,Arrays,Multidimensional Array,我正在尝试设置一个基本的错误检查系统,它将捕获由系统调用运行的shell错误execute_command是一个webmin函数,它运行系统调用,然后将错误消息设置为其第四个参数。我基本上调用execute\u command\u error(“adduser test”),知道我已经创建了一个名为test的用户,并且基于预定义的数组,我希望它能够打印出来 无法添加用户无法添加 添加该用户,因为它已经 存在于系统上 但我得到的却是: 嗯?'val'}会创建一个哈希引用,因此您可以在查找键之前取消

我正在尝试设置一个基本的错误检查系统,它将捕获由系统调用运行的shell错误execute_command是一个webmin函数,它运行系统调用,然后将错误消息设置为其第四个参数。我基本上调用execute\u command\u error(“adduser test”),知道我已经创建了一个名为test的用户,并且基于预定义的数组,我希望它能够打印出来

无法添加用户
无法添加 添加该用户,因为它已经 存在于系统上

但我得到的却是:

嗯?<嗯,嗯

我已经验证了$exe和$return分别是“adduser”和1。 我不了解阵列的哪些方面?它似乎忽略了字符串和或数字,只使用最后一个包含3个元素的定义。这个问题的解决方案是什么,还是更好的解决方案

以下是代码:

$ErrorMsg['adduser',1,'title']  = "Unable to add user";
$ErrorMsg['adduser',1,'msg']    = "Unable to add that user because it already exists on the system.";
$ErrorMsg['random',2,'duaisdhai']  = "Uhhhhhhhhh?";

sub execute_command_error
{
    my $error = "";
    my $cmd = $_[0];

    $return = execute_command($cmd, undef, undef, \$error)>>8;
    if ($error) {
        my ($exe) = $cmd =~ m|^(.*?)[ ]|;

        $exe_title = $ErrorMsg[$exe,$return,'title'];
        $exe_msg = $ErrorMsg[$exe,$return,'msg'];


        print $exe_title."<br>";
        print $exe_msg ."<br>";
    }
}
现在我如何使用变量引用它?因为这两种方法都不起作用:

    $exe_title = $ErrorMsgs{"$exe"}{"$return"}{"title"};
    $exe_title = $ErrorMsgs{$exe}{$return}{title};
首先,请参阅以了解执行多维结构的正确语法。你的数组没有任何意义

如果启用此选项,您将看到“参数不是数字”警告,告诉您不能在数组索引中以任何有意义的方式使用字符串

但是你在更新中发布的哈希应该可以正常工作

#!/usr/bin/perl

use strict;
use warnings;
## ^^ these things are your friends

my %ErrorMsgs =    ('adduser' =>   {
                        '1' =>  {
                                'title' =>      'Unable to add user',
                                'msg'   =>      'Unable to add that user because it already exists on the system.',
                        },
                },
                );

my $exe = 'adduser';
my $return = 1;

print $ErrorMsgs{$exe}{$return}{title};    # works

如果您没有得到预期的输出,那是因为
$exe
$return
有问题——它们可能未在您尝试使用它们的范围内定义。打开并发出警告将有助于跟踪问题。

{'key'=>'val'}会创建一个哈希引用,因此您可以在查找键之前取消引用

$exe_title = $ErrorMsgs{$exe}->{$return}->{"title"};
您也不需要引用$exe或$return,因为它们已经包含字符串

注意Perl不支持多维索引;多维数组只是数组的数组,因此需要对每个索引使用[]。在标量上下文中,逗号运算符返回最右边表达式的值,因此以下行是等效的:

$ErrorMsg[0,1,2]  = "foo";
$ErrorMsg[2]  = "foo";
请注意,在列表上下文中,逗号运算符返回一个值列表,这为我们提供了切片:

@a=qw(f o o);
@a[3,4,5] = qw(b a r);
print join(',', @a), "\n";
# output: f,o,o,b,a,r 
@ErrMsg{qw(title msg)} = ('Unable to add user', 'Unable to add that user because it already exists on the system.')
@a=qw(f o o);
@a[3,4,5] = qw(b a r);
print join(',', @a), "\n";
# output: f,o,o,b,a,r 
@ErrMsg{qw(title msg)} = ('Unable to add user', 'Unable to add that user because it already exists on the system.')