Tania
Tania

Reputation: 428

Returning multiple values from embedded Perl script

I have a Perl script embedded in a C program. I want to return an array of integers from the Perl script. However, the number n of integers to be returned, is an input to the program and cannot be hardcoded in the Perl script. Is there a way to do this? Here are a few examples:

Example 1 (n is known and equal to 2 in PERL subroutine):

@num = {1, 2, 3, 4};
($num[0], $num[1]); // works, returns the two values 

Example 2 (n is not known):

@num = {1, 2, 3, 4}; 
(@num); // does not work

Example 3 (n is not known):

@num = {1, 2, 3, 4};
$string = "($num[0], $num[1], $num[2], $num[3])";
$string; // does not work

Upvotes: 2

Views: 186

Answers (1)

Gilles Quénot
Gilles Quénot

Reputation: 185530

Take care, { } is used for HASH references. I think you simply need :

my @num = qw/1 2 3 4/;
@num;

or

my @num = (1, 2, 3, 4);
@num;

or

my @num = (1..4);
@num;

or usig an ARRAY ref :

my $num = [1, 2, 3, 4];
@$num;

Upvotes: 3

Related Questions