Dear Me,
thanks for the great explanations! There is definitely MTOWTDI.
Shame I found one of the ways NOT to do it (but of course there are
infinitely more of those, so it's easier 8-). I'll examine each of your in
more detail and try each of them out - thus, hopefully, I'll learn some
more.
I read perlop and the Camel book including the part about the scalar context
and, truthfully, didn't really understand it, although a little bell was
quietly ringing.
As you say, I need an assertion that $state is within the range 0-3. The
code I sent is really a just a 'distillation' of what I'm trying to do. The
part I sent is within a subroutine and is called with the $state and $time
parms already checked by the calling code.
Looking quickly at your suggestions, it's your first that seems to fit my
needs.
Many thanks again for your time and explanations.
Murray Webster
-----Original Message-----
From: Me [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, 4 July 2001 16:12
To: Webster, Murray; [EMAIL PROTECTED]
Subject: Re: Match range of array elements?
> 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.