如何将列表中的每个元素作为函数的参数传递

时间:2017-08-01 08:16:28

标签: perl remedy

我的数据在数组中。我需要将一个数组值传递给ars_CreateEntry函数 ARSperl。 数组是根据收到的输入动态形成的,因此其长度可能会有所不同。

#!/usr/bin/perl

use strict;

use ARS;
use ARS::Login;

my $server   = '125.10.14.1';
my $username = 'RemedyUser';
my $dbsid    = $ENV{'ORACLEID'};
my $dbuser   = 'admin';
my $schema   = 'Form 1';

my $control = remedyars_login( $username )
        or die "ERROR: Cannot login to $server \n";
my $dbh = remedydbi_login( $dbsid, $dbuser )
        or die( "Cannot connect to $dbsid: $DBI::errstr\n" );

my @arr = ( 1, "New", 2, "Test", 3, "Hi" );
my $created_ticket = ars_CreateEntry( $control, $schema, join( ",", @arr ) )
        or die "ERROR: Cannot create host ticket: $ars_errstr\n";

ars_Logoff( $control );

当我运行脚本时,我收到此错误

  

错误:无法创建主票:[错误]参数数量无效(ARERR#80000)

1 个答案:

答案 0 :(得分:2)

直接使用数组(参见ars_CreateEntry examples):

my @arr=(1,"New",2,"Test",3,"Hi");
die unless @arr % 2 == 0; # enforce even number of elements in @arr
my $created_ticket=ars_CreateEntry( $control, $schema, @arr) 
  or die "ERROR: Cannot create host ticket: $ars_errstr\n";

如果ars_CreateEntry对参数的顺序不挑剔,甚至更好地使用哈希而不是数组:

my %params=(1 => "New",2 => "Test", 3 => "Hi");
# hash will be converted to array
my $created_ticket=ars_CreateEntry( $control, $schema, %params) 
  or die "ERROR: Cannot create host ticket: $ars_errstr\n";