Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Passing an array to a function, where each element is an argument?

Reply
Thread Tools

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

 
 
Tony
Guest
Posts: n/a
 
      02-23-2004
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

???
 
Reply With Quote
 
 
 
 
Dmitry Roslyakov
Guest
Posts: n/a
 
      02-23-2004
Tony wrote:

> 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);
 
Reply With Quote
 
 
 
 
fifo
Guest
Posts: n/a
 
      02-23-2004
At 2004-02-23 13:13 -0800, Tony wrote:
> 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:umper;
sub row { print Dumper \@_ };
@VALS = qw/one two three/;
row('zero', @VALS);

prints

$VAR1 = [
'zero',
'one',
'two',
'three'
];
 
Reply With Quote
 
Beable van Polasm
Guest
Posts: n/a
 
      02-24-2004
(Tony) writes:

> 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__


--

 
Reply With Quote
 
Tore Aursand
Guest
Posts: n/a
 
      02-24-2004
On Mon, 23 Feb 2004 13:13:48 -0800, Tony wrote:
> 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.


--
Tore Aursand <>
"Time only seems to matter when it's running out." -- Peter Strup
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
write a program to count occurence of each element in an array sahil C++ 4 05-28-2008 02:21 PM
how to Update/insert an xml element's text----> (<element>text</element>) HANM XML 2 01-29-2008 03:31 PM
Test equality of each element in an array 12 34 Ruby 15 07-03-2007 01:48 AM
How to compute the number of occurance of each element in an array list? John Java 16 05-05-2007 12:29 PM
Array#each - getting each element and the index Pat Maddox Ruby 6 01-20-2006 04:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57