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

Discussion in 'Perl Misc' started by Tony, Feb 23, 2004.

  1. Tony

    Tony Guest

    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

    ???
     
    Tony, Feb 23, 2004
    #1
    1. Advertising

  2. 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);
     
    Dmitry Roslyakov, Feb 23, 2004
    #2
    1. Advertising

  3. Tony

    fifo Guest

    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::Dumper;
    sub row { print Dumper \@_ };
    @VALS = qw/one two three/;
    row('zero', @VALS);

    prints

    $VAR1 = [
    'zero',
    'one',
    'two',
    'three'
    ];
     
    fifo, Feb 23, 2004
    #3
  4. (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__


    --
     
    Beable van Polasm, Feb 24, 2004
    #4
  5. Tony

    Tore Aursand Guest

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

    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
     
    Tore Aursand, Feb 24, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Reckoner
    Replies:
    11
    Views:
    704
    Steven D'Aprano
    Jan 19, 2009
  2. Antonio Vera
    Replies:
    2
    Views:
    225
    Prasad, Ramit
    Aug 9, 2011
  3. AikidoGuy
    Replies:
    11
    Views:
    560
    Seebs
    Nov 21, 2011
  4. Pat Maddox
    Replies:
    6
    Views:
    158
    Marcin Mielżyński
    Jan 20, 2006
  5. User1014
    Replies:
    1
    Views:
    186
    Richard Cornford
    Nov 30, 2006
Loading...

Share This Page