in reply to Assigning the Length of an Array to a Variable

I am trying to define a variable which has a value equal to the length of an array

Easy enough, array in scalar context returns the size of the array (number of elements, the "length")

The part that is giving me trouble, I want the length to vary as I read through the array one element at a time.

WHAT? Unless you add/remove elements from the array, the size cannot change

You 're not looking for the size/length of an array

YOu 're not looking for the last index of the array

You appear to be looking for the current index of an array, in which case you have to use a c-style for-loop, or if your perl is new enough, each array

$ perl -MData::Dump -e " @f=a..c; while(@g=each @f){ dd\@g } " [0, "a"] [1, "b"] [2, "c"]

Replies are listed 'Best First'.
Re^2: Assigning the Length of an Array to a Variable (INDEX)
by Anonymous Monk on Aug 26, 2013 at 05:06 UTC

    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?

      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
      Maybe, but I wouldn't dare try without an example iteration or two