in reply to Changing each item in an array

Your @array variable contains three strings: '$one', '$two', and '$three'. Try it again as my @array = ($one, $two, $three). And pick better names. Those are atrocious and are absolutely uncommunicative about their purpose.

Added: I should mention that doing things this way means the copy of the value in @array is now altered, the original contents of $one, $two and $three will remain unaltered. You need references to alter the original variables. Your code then looks like this:

my @array = ( \ $one, \ $two, \ $three); foreach my $item (@array){ if ($$item) { $$item = 'yes'; } else { $$item = 'no'; } }

Replies are listed 'Best First'.
Re: Re: Changing each item in an array
by Limbic~Region (Chancellor) on Jul 19, 2003 at 00:03 UTC
    diotalevi,
    I know that this was probably a contrived example by the Anonymous Monk to illustrate the problem, but in this case I would probably:
    for my $item ($one, $two, $three) { $item = $item ? 'yes' : 'no'; }
    This gets rid of the array all together and the need to dereference. I realize that the for loop is magically creating the reference and doing the dereference implicitly aliasing for you, but it is transparent. Hmmm - may I should get rid of that ternary operator if my point was for simplicity/clarity to the Anonymous Monk - nah!

    Cheers - L~R
    updated: Modified verbiage per diotalevi's clarification

      No, foreach doesn't create a reference and dereference for you. Really. Its an internals thing and it *aliases* the loop variable. This is really outside the bounds of what I think will confuse the original poster though. I also would normally have used ternary assignment but, again, in the interests of speaking to the original poster's knowledge level I kept the same if/else syntax and just changed the bits that needed to be.