bory has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have more arrays, each array having a variable number of elements. The elemnts of the array come from a form with multiple select list.With every element of the arrais I want to do smth like this:
@State=("a","b","c")#the number of elements is variable @Product=("12","13","15") What I want to do is creating an expresion like this: "create $Product[0]&&$Product[1]&&$Product[2]...$Product[n] -$State[0] +&&$State[1]&&$State[2]...&&$State[i]" n=number of elements of the array Product i=number of elements of the array State
My problem is that i relly don't know how to do this because of the variable number of the elements! Thank you very much for your time.

Replies are listed 'Best First'.
Re: elements of an array
by dreadpiratepeter (Priest) on Oct 27, 2003 at 16:27 UTC
    If I understand the question right, you want:
    "create " . join('&&',@Product) . " -" . join('&&',@State)
    join flattens an array into a string, the first argument is the separator to use.


    -pete
    "Worry is like a rocking chair. It gives you something to do, but it doesn't get you anywhere."
Re: elements of an array
by QM (Parson) on Oct 27, 2003 at 16:35 UTC
    To create the string, use join, as in:
    my $string = join('&&', @Product) . ' -' . join('&&', @State);
    If create is a function, then
    create($string);
    -QM
    --
    Quantum Mechanics: The dreams stuff is made of
Re: elements of an array
by davido (Cardinal) on Oct 27, 2003 at 18:49 UTC
    Is it possible that what you mean is that you would like to subtract $State[i] from $Product[n], for each element of @State? There is more than one way to do that, but here is one way using map that I happen to like:

    my @array = map { $Product[$_] - $State[$_] } 0..$#State;

    Of course I could be misinterpreting your question entirely. Others have assumed that you mean you want to join all elements together. For that, you use join. But if your intention is to subtract each element of @State from the element of the same index number from @Product, this is how it's done. I only mention it because I see that minus sign in your example.


    Dave


    "If I had my life to live over again, I'd be a plumber." -- Albert Einstein