Line 9 says
else { $num * 3; $num++; }
The expression "$num * 3" is in a void context. In other words, it
calculates a value, but doesn't assign that value to anything. I think the
most likely typo is a missing equals sign:
else { $num *= 3; $num++; }
^
This would multiply $num times 3 and store the result in $num.
Mark Nutter
Manager, Internet Applications Development
Marconi
[EMAIL PROTECTED]
It's not necessarily an advantage to have better brakes than the guy behind
you.
> -----Original Message-----
> From: McCormick, Rob E [mailto:[EMAIL PROTECTED]]
> Sent: Wednesday, April 25, 2001 5:47 PM
> To: '[EMAIL PROTECTED]'
> Subject: void context, looping example
>
>
>
> I'm trying to understand an example from the Waite Press book (Perl 5
> Interactive Course, a bit dated, '97, Orwant) The example
> doesn't have -w
> or strict; enabled. I ran it with them and the error follows:
>
> #!c:/perl/bin/perl -w
> use strict;
>
> print "Please type an integer: ";
> chomp ( my $num = <STDIN>);
> while ( $num != 1 ) {
> print "$num";
> if ( ( $num % 2 ) == 0 ) { $num /= 2 } # so if it's
> even, or divisible
> by 2, divide by 2
> else { $num * 3; $num++; } #multiply the number
> entered by 3(?), then
> increment(?)
> }
> print $num, "\n"; # I tried with and without the \n and the
> output never
> rendered differently?
> #push (my @numbers, $num); print '@numbers';
>
> Error: Useless use of multiplication in void context at
> C:\WINNT\Profiles\moi\Desktop\perl\lesson4\mea
> nder.pl line 9.
> Please type an integer:
>
> Please type an integer: 35
> 353618910563421
>
> The example is simply there to teach a looping construct, (it
> could be just
> a bad example..I doublechecked my typing, but my error is
> invisible to me at
> this point....)
>
> ?'s:
> What's void context?. I've seen it referenced using perldoc
> -q void, but
> don't quite follow the answers in perldoc?
>
> thanks in advance,
> Rob
> --
> Rob McCormick
> GlaxoSmithKline
> [EMAIL PROTECTED]
>
>