>    if ($printLine[$state..3] =~ /^----/)

1. The syntax $foo[....] signifies a single element of array @foo.
The ..... bit is in scalar context and should evaluate to a *single*
*numeric* value. Hence the complaint:

    Argument "" isn't numeric in array element


2. When you use a variable that isn't defined, and you have
warnings switched on, perl will complain. For some lines of
your TEST file, $state ends up not defined, hence:

    Use of uninitialized value in pattern match (m//)

The line number for the specific lines is given in the error message.


To make matters worse:

3. When you use the X..Y syntax in a scalar context, the ..
operator is the bistable (flipflop) operator, not the range
operator.

4. The =~ operator expects a single value on the left side.


You clearly ain't doing what you want to do.

I don't know what happens earlier in your script (and given
the errors above, I ain't too confident you've gotten it right),
but to match against a range of array elements, how you
might want to try:

    if (grep { /^---/ } @printLine[$state..3]) {;

to match any, or

    if ($state..3 == grep { /^---/ } @printLine[$state..3]) {;

to match all. You should add an assert that $state is (or
evaluates to) an integer between 0 and 3. And both of
these are somewhat obfuscated coding. Perhaps:

    # to match any
    my $match = 0;
    for ($state..3) {
        $match ||= $printLine[$_] =~ /^---/
    }
    if ($match) {

or

    # to match all
    my $match = 1;
    for ($state..3) {
        $match &&= $printLine[$_] =~ /^---/
    }
    if ($match) {

would be better, though note that the latter indicates
you have matched all if $state isn't in the range 0-3.

hth.

Reply via email to