Looks like they are what translates the embedded arithmetical symbols
(in $line as a text string) into perl arithmetical operators, performs
the indicated calculation and prints the result. The reason for 0 <
index() is to ensure that the arithmetical symbols don't begin the line
which is part the method for verifying the syntax of the line in
combination with the position requirements of $a and $b. I think the
idea is only to print the calculation of translated lines that have
formats like:
(number a)(first space)(...)(+ or *)(...)(last space)(number b)
Where the (...) are probably meant to be null but can be any sequencs of
chars, and there is no check that (number a) and (number b) are
sequences only of digits 0-9.
It would be better to use a regexp to test the pattern of $line and
print out an informative message if it doesn't conform to what is needed
for later processing - something like:
$line = <STDIN>;
chomp $line;
If ( $line =~ /^\d+\s+[\+\*]\s+\d+$/ ) {
$first_space = index($line , " ");
$a = substr($line, 0, $first_space);
$last_space = rindex($line , " ");
$b = substr($line, $last_space+1);
if (0 < index($line, "*")) {
print $a * $b, "\n";
}
if (0 < index($line, "+")) {
print $a + $b, "\n";
}
} else {
Print "malformed line: $line\n";
}
See perldoc perlre to tune the regexp test of $line just the way you
need it.
- tristram nefzger
-----Original Message-----
From: Ronen Kfir [mailto:[EMAIL PROTECTED]
Sent: Wednesday, October 01, 2003 9:43 AM
To: [EMAIL PROTECTED]
Subject: RE: index
Importance: High
Sorry about the last one. I wrote some test & somehow it flipped away!
The following script meant to calculate simple math drills, such as 5 *
9 or 4 + 3, written in one line. What I cannot comprehend is the lines
marked with ---. What do they do? Why is there a "0 < index..."
Oh, it all makes sense now......... what are you going on about man?
> Hi,
>
>
$line = <STDIN>;
chomp $line;
$first_space = index($line , " ");
$a = substr($line, 0, $first_space);
$last_space = rindex($line , " ");
$b = substr($line, $last_space+1);
--- if (0 < index($line, "*")) {
print $a * $b, "\n";
}
--- if (0 < index($line, "+")) {
print $a + $b, "\n";
}
________________________________________________________________________
This email has been scanned for all viruses by the MessageLabs Email
Security System. For more information on a proactive email security
service working around the clock, around the globe, visit
http://www.messagelabs.com
________________________________________________________________________
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]