in reply to Re: Re: Initialize array
in thread Initialize array

Ok. Let me see if I got this right - you're complaining that if you enter 7 at home, it will print 6, but that if you enter 7 at work, it will print 7 ... ?

First off, the indices of an array go from 0 to $#lines. So, when you do $number1 <= 0 as a boundary condition, you're already cutting off one of your useable indices.

The second thing is that you're comparing the number they give you (from 1 .. N) to a range from (0 .. N-1). I would do a $number1--; right after the chomp.

The third thing is that you're using | (the bit-wise OR) when I think you mean to be using || (the logical OR).

A few style notes:

Hope this helps!

Replies are listed 'Best First'.
Re4: Initialize array
by Hofmator (Curate) on Jul 04, 2001 at 18:52 UTC

    Very good answer, dragonchild++! Let me add a small remark to your remark on \s* in pattern matching regexes.

    Probably what was meant was a line starting with a 'r' indented or not (assuming that we are looking at a textfile)

    # was if (!($line =~ /\s*[a-qs-zA-Z]/)) # equivalent to (as noted by dragonchild) unless ($line =~ /[a-qs-zA-Z]/) # should have been IMHO unless ($line =~ /^\s*[a-qs-zA-Z]/) # which is equivalent to (with assumption 'textfile') if ($line =~ /^\s*r/)
    Note the ^ indicating the beginning of the line. In that case the \s* is necessary to skip any indentation at the start of the line. The last statement is (in my view) much clearer - providing of course I guessed correctly at the intention of Myia ;-)

    To sum up, if you are using an anchor (like ^, $ or \G) in your pattern then \s* might be useful in a match, otherwise it isn't.

    -- Hofmator