perlremedy

How to pass each element in a list as arguments of a function


I have my data in an array. I need to pass an array value to ars_CreateEntry function in ARSperl. The array is formed dynamically based on inputs received, so its length may vary.

#!/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 );

When I run the script I get this error

ERROR: Cannot create host ticket: [ERROR] Invalid number of arguments (ARERR #80000)


Solution

  • Use the array directly (see 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";
    

    Or even better use hash instead of array if ars_CreateEntry is not picky about order of parameters:

    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";