Useless use of private variable

--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."=0A=0A=
=0AIf I comment out the "for loop" like this the warning goes away...=0A#fo=
r($i; $i < $n; $i++)=0A#{=0A=A0=A0=A0 push ( [at] some_array, $i);=0A#}=0A=0AWha=
t does the warning mean and how do I get rid of it?=0A=0A#!/usr/bin/perl=0A=
use strict;=0Ause warnings;=0A=0Asub get_some_array=0A{=0A=A0=A0=A0 my $n =
=3D shift; #number of elements=0A=A0=A0=A0 my $i =3D 0; #loop counter=0A=A0=
=A0=A0 my [at] some_array;=0A=A0=A0=A0 =0A=A0=A0=A0 if(defined($n))=0A=A0=A0=A0=
{=0A=A0=A0=A0 =A0=A0=A0 for($i; $i < $n; $i++)=0A=A0=A0=A0 =A0=A0=A0 {=0A=
=A0=A0=A0 =A0=A0=A0 =A0=A0=A0 push ( [at] some_array, $i);=0A=A0=A0=A0 =A0=A0=A0=
}=0A=A0=A0=A0 }=0A=A0=A0=A0 return [at] some_array;=0A}=0A=0Amy [at] some_array =
=3D get_some_array (8);=0Amy $comma =3D "";=0Afor my $ele ( [at] some_array)=0A{=
=0A=A0=A0=A0 print $comma . $ele;=0A=A0=A0=A0 $comma =3D ", ";=0A}=0Aprint =
"\n";=0A=0A--=0ARon=0AThis is perl, v5.10.1 (*) built for i686-linux-gnu-th=
read-multi
--0-1149359479-1313849509=:56926--
Ron Weidner [ Sa, 20 August 2011 16:11 ] [ ID #2063467 ]

Re: Useless use of private variable

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 [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Alan Haggai Alavi [ Sa, 20 August 2011 16:29 ] [ ID #2063468 ]

Re: Useless use of private variable

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 ( [at] some_array, $i);
> #}

You could change that to:

for ( my $i = 0; $i < $n; $i++ )
{
push [at] some_array, $i;
}

Or:

for my $i ( 0 .. $n - 1 )
{
push [at] some_array, $i;
}

Or simply:

push [at] 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 [at] some_array = 0 .. $n - 1 if defined $n;
return [at] 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 [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
jwkrahn [ Sa, 20 August 2011 16:36 ] [ ID #2063469 ]
Perl » gmane.comp.lang.perl.beginners » Useless use of private variable

Vorheriges Thema: searching the array
Nächstes Thema: why si this code not working (variable substitution)