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

Hi Perl Monks! I am trying to define a variable which has a value equal to the length of an array. The part that is giving me trouble, I want the length to vary as I read through the array one element at a time. This is what I thought to do

open(MASK, "/Users/logancurtis-whitchurch/Dropbox/thesis_folder/consen +sus_files/filtered_mask_files/filtered.mask."."$population".".txt") o +r die "can't open masked file\n"; my $mask = <MASK>; while (($n = read MASK, $data, 100000) != 0) { my @info = split(//, $data); foreach $info (@info){ if ($#info[$position] < $start) { #change the element at that position to an 'N' substr($mask,($position),1,'N'); $position++; } if (($#info[$position] >= $start) && ($#info[$position] <= $en +d) { $position++; } if ($#info[$position] > $end) { $count++;
but terminal returns an error message: syntax error at filter.masks.pl line 55, near "$#info["

Is there a proper way write a variable that equals the current length of the array, at the position equal to the value of my incrementing 'position' variable?

Replies are listed 'Best First'.
Re: Assigning the Length of an Array to a Variable
by toolic (Bishop) on Aug 25, 2013 at 22:58 UTC
    $#info gives the index of the last element of the @info array. See perldata

    $info[$position] gives the array element at $position. Trying to combine those 2 results in a syntax error.

    It might be a good idea to use different names for your info scalar variable and info array variable.

    Maybe you want:

    my $len = length $info[$position];
Re: Assigning the Length of an Array to a Variable (INDEX)
by Anonymous Monk on Aug 25, 2013 at 23:46 UTC

    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"]

      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