in reply to Re: Assigning the Length of an Array to a Variable (INDEX)
in thread Assigning the Length of an Array to a Variable

Is it possible then to write something with a foreach loop that says, foreach element of my array if its index value is less than my start variable change its value to 'N' then move to the next element?

  • Comment on Re^2: Assigning the Length of an Array to a Variable (INDEX)

Replies are listed 'Best First'.
Re^3: Assigning the Length of an Array to a Variable (INDEX)
by AnomalousMonk (Archbishop) on Aug 26, 2013 at 07:20 UTC

    Some ways (by no means every possible way) to do what I understand you to want done:

    >perl -wMstrict -le "my @t = ('A' .. 'M'); ;; my @ra = @t; print qq{@ra}; ;; my $start = 5; for my $i (0 .. $#ra) { if ($i < $start) { $ra[$i] = 'N'; } } print qq{@ra \n}; ;; @ra = @t; print qq{@ra}; ;; my $end = ($start <= $#ra) ? $start - 1 : $#ra; for my $i (0 .. $end) { $ra[$i] = 'N'; } print qq{@ra \n}; ;; @ra = @t; print qq{@ra}; $_ = 'N' for @ra[ 0 .. $end ]; print qq{@ra \n}; ;; @ra = @t; print qq{@ra}; @ra[ 0 .. $end ] = ('N') x ($end + 1); print qq{@ra \n}; " A B C D E F G H I J K L M N N N N N F G H I J K L M A B C D E F G H I J K L M N N N N N F G H I J K L M A B C D E F G H I J K L M N N N N N F G H I J K L M A B C D E F G H I J K L M N N N N N F G H I J K L M
Re^3: Assigning the Length of an Array to a Variable (INDEX)
by Anonymous Monk on Aug 26, 2013 at 05:41 UTC
    Maybe, but I wouldn't dare try without an example iteration or two