Useless use of private variable
am 20.08.2011 16:11:49 von Ron Weidner
--0-1149359479-1313849509=:56926
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
In the program below the "for loop"=A0 is causing the warning...=0A"Useless=
use of private variable in void context at ./uselessUse.pl line 16."
=
=0AIf I comment out the "for loop" like this the warning goes away...=0A#fo=
r($i; $i < $n; $i++)=0A#{
push (@some_array, $i);=0A#}
Wha=
t does the warning mean and how do I get rid of it?
#!/usr/bin/perl=0A=
use strict;=0Ause warnings;
sub get_some_array=0A{
my $n =
=3D shift; #number of elements
my $i =3D 0; #loop counter
=
my @some_array;
if(defined($n))
=
{
=A0 for($i; $i < $n; $i++)
=A0 {=0A=
=A0 =A0 =A0 push (@some_array, $i);
=A0=
}
}
return @some_array;=0A}
my @some_array =
=3D get_some_array (8);=0Amy $comma =3D "";=0Afor my $ele (@some_array)=0A{=
print $comma . $ele;
$comma =3D ", ";=0A}=0Aprint =
"\n";
--=0ARon=0AThis is perl, v5.10.1 (*) built for i686-linux-gnu-th=
read-multi
--0-1149359479-1313849509=:56926--
Re: Useless use of private variable
am 20.08.2011 16:29:43 von Alan Haggai Alavi
Hello Ron,
> for($i; $i < $n; $i++)
When Perl evaluates the initialiser of the for loop, it just sees a `$i`.
There are no effects for such a statement. Hence the warning. For getting more
details about warnings, you can use the diagnostics pragma. (See `perldoc
diagnostics`)
Since you have already initialised $i to 0 (my $i = 0;), you could omit the
initialiser of the for loop:
for ( ; $i < $n; $i++ )
Otherwise, as is normal, you can use the for loop's initialiser section to
declare and then initialise $i to 0:
for ( my $i = 0; $i < $n; $i++ )
Your program can be rewritten as:
print join ', ', 0 .. 7;
Regards,
Alan Haggai Alavi.
--
The difference makes the difference.
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/
Re: Useless use of private variable
am 20.08.2011 16:36:13 von jwkrahn
Ron Weidner wrote:
> In the program below the "for loop" is causing the warning...
> "Useless use of private variable in void context at ./uselessUse.pl line 16."
>
>
> If I comment out the "for loop" like this the warning goes away...
> #for($i; $i< $n; $i++)
^^
The first $i is in void context
> #{
> push (@some_array, $i);
> #}
You could change that to:
for ( my $i = 0; $i < $n; $i++ )
{
push @some_array, $i;
}
Or:
for my $i ( 0 .. $n - 1 )
{
push @some_array, $i;
}
Or simply:
push @some_array, 0 .. $n - 1;
So you could write your subroutine more simply as:
sub get_some_array
{
my $n = shift; #number of elements
my @some_array = 0 .. $n - 1 if defined $n;
return @some_array;
}
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/