Passing an array to a function, where each element is an argument?

T

Tony

I have a function that expects a comma seperated list of values.

Is there some way to pass it an array, where each element is treated
as a distinct value?

For example, I have an array @VALS with values: "one", "two, "three".

Can I call "row" in a way such that it appears to have been called
like so:

$rtf->row($arowdef,"one","two","three");

.... without knowing how many elements are in @VALS

???
 
D

Dmitry Roslyakov

Tony said:
I have a function that expects a comma seperated list of values.

Is there some way to pass it an array, where each element is treated
as a distinct value?

For example, I have an array @VALS with values: "one", "two, "three".

Can I call "row" in a way such that it appears to have been called
like so:

$rtf->row($arowdef,"one","two","three");

... without knowing how many elements are in @VALS

???

Try something like this:

$rtf->row($arowdef, @VALS);
 
F

fifo

I have a function that expects a comma seperated list of values.

Is there some way to pass it an array, where each element is treated
as a distinct value?

For example, I have an array @VALS with values: "one", "two, "three".

Can I call "row" in a way such that it appears to have been called
like so:

$rtf->row($arowdef,"one","two","three");

... without knowing how many elements are in @VALS

???

Maybe I don't understand the question, but what's wrong with just
calling the function with @VALS:

use Data::Dumper;
sub row { print Dumper \@_ };
@VALS = qw/one two three/;
row('zero', @VALS);

prints

$VAR1 = [
'zero',
'one',
'two',
'three'
];
 
B

Beable van Polasm

I have a function that expects a comma seperated list of values.

Is there some way to pass it an array, where each element is treated
as a distinct value?

For example, I have an array @VALS with values: "one", "two, "three".

Can I call "row" in a way such that it appears to have been called
like so:

$rtf->row($arowdef,"one","two","three");

... without knowing how many elements are in @VALS

Yes of course. Did you try this?

$rtf->row($arowdef, @VALS);

Example:

#!/usr/bin/perl

use strict;
use warnings;

my @VALS = qw / one two three /;
my $arowdef = "abc";

xxx($arowdef, @VALS);

sub xxx
{
my @args = @_;

foreach my $arg(@args)
{
print("arg = $arg\n");
}
}

__END__


--
 
T

Tore Aursand

I have a function that expects a comma seperated list of values.

I guess you mean an array?
Is there some way to pass it an array, where each element is treated
as a distinct value?

For example, I have an array @VALS with values: "one", "two, "three".

Can I call "row" in a way such that it appears to have been called
like so:

$rtf->row($arowdef,"one","two","three");

... without knowing how many elements are in @VALS

Yes. A sub-routine always expects a number of values, which are
represented in '@_';

sub row {
my @values = @_;
}

You also mentioned that you wanted the array to be unique. Take a look at
'perldoc -q duplicate' for more information on that issue.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top