2

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
1
  • 1
    If it was helpful, please accept sputnick's answer by clicking on the green-white tick to the left from it. Commented Dec 4, 2014 at 1:28

1 Answer 1

3

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.