[33139] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 4417 Volume: 11

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sun Apr 19 05:17:23 2015

Date: Sun, 19 Apr 2015 02:17:07 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Sun, 19 Apr 2015     Volume: 11 Number: 4417

Today's topics:
    Re: format file lancerset@gmail.com
    Re: format file lancerset@gmail.com
    Re: format file lancerset@gmail.com
    Re: format file <JimSGibson@gmail.com>
    Re: format file <rweikusat@mobileactivedefense.com>
    Re: format file <JimSGibson@gmail.com>
    Re: format file <hjp-usenet3@hjp.at>
        Uninitialized value, but it shouldn't be <ams@ludd.ltu.se>
    Re: Uninitialized value, but it shouldn't be <derykus@gmail.com>
    Re: Uninitialized value, but it shouldn't be <gamo@telecable.es>
    Re: Uninitialized value, but it shouldn't be <ams@ludd.ltu.se>
    Re: Uninitialized value, but it shouldn't be <ams@luminous.ludd.ltu.se>
    Re: Uninitialized value, but it shouldn't be <gamo@telecable.es>
    Re: Uninitialized value, but it shouldn't be <rweikusat@mobileactivedefense.com>
    Re: Uninitialized value, but it shouldn't be <rweikusat@mobileactivedefense.com>
    Re: Uninitialized value, but it shouldn't be <rweikusat@mobileactivedefense.com>
    Re: Uninitialized value, but it shouldn't be <gamo@telecable.es>
    Re: Uninitialized value, but it shouldn't be <whynot@pozharski.name>
    Re: Uninitialized value, but it shouldn't be <gamo@telecable.es>
    Re: Uninitialized value, but it shouldn't be <gamo@telecable.es>
    Re: Uninitialized value, but it shouldn't be <hjp-usenet3@hjp.at>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Fri, 17 Apr 2015 19:11:54 -0700 (PDT)
From: lancerset@gmail.com
Subject: Re: format file
Message-Id: <e6d6d6c7-f173-463b-8e4b-955ebd5e1553@googlegroups.com>

On Friday, April 17, 2015 at 4:13:04 PM UTC-4, George Mpouras wrote:
> On 17/4/2015 10:34 =CE=BC=CE=BC,  wrote:
> > And if it looks like 8776a653-bd56-97a4-65c3-6543a767656a  ?
> >
>=20
> its ok but then you have to change the spaces as
>=20
>=20
> $mask  =3D join '', map { "%-40s" } 0..$#names;
> printf STDOUT  "$mask\n"  ,@names;
> $mask  =3D join '', map {"%-30s%-10s"} 0..$#names;
>=20
>=20
>=20
> Are you sure you do not need a proper formatting tool as Rainer said=20
> using e.g the Template ?
> https://metacpan.org/pod/Template
> But this is a task you should try your self , as using this module have=
=20
> almost nothing to do with Perl or programming

this is works perfectly. One more question. Sorry to be annoying. i'm openi=
ng the end result in excel. Is there a way in this script that the entries =
would be considered 1 column

_______________________________________
0157a353-fa74-4a32-ba4d-40c5623623ef   |
1429283609                    0        |    =20
1429283608                    5        |
 ...                          ...          |
_______________________________________|                                   =
  


------------------------------

Date: Fri, 17 Apr 2015 19:14:01 -0700 (PDT)
From: lancerset@gmail.com
Subject: Re: format file
Message-Id: <5be56a46-b67b-4f09-bd24-ff537abade77@googlegroups.com>

On Friday, April 17, 2015 at 4:13:04 PM UTC-4, George Mpouras wrote:
> On 17/4/2015 10:34 =CE=BC=CE=BC, wrote:
> > And if it looks like 8776a653-bd56-97a4-65c3-6543a767656a  ?
> >
>=20
> its ok but then you have to change the spaces as
>=20
>=20
> $mask  =3D join '', map { "%-40s" } 0..$#names;
> printf STDOUT  "$mask\n"  ,@names;
> $mask  =3D join '', map {"%-30s%-10s"} 0..$#names;
>=20
>=20
>=20
> Are you sure you do not need a proper formatting tool as Rainer said=20
> using e.g the Template ?
> https://metacpan.org/pod/Template
> But this is a task you should try your self , as using this module have=
=20
> almost nothing to do with Perl or programming

this is works perfectly. One more question. Sorry to be annoying. i'm openi=
ng the end result in excel. Is there a way in this script that the entries =
would be considered 1 column when open in excel?
=20
0157a353-fa74-4a32-ba4d-40c5623623ef   =20
1429283609                    0            =20
1429283608                    5        =20
 ...


------------------------------

Date: Fri, 17 Apr 2015 19:16:42 -0700 (PDT)
From: lancerset@gmail.com
Subject: Re: format file
Message-Id: <cd929103-96aa-41f2-b2f6-f1e23d2e9dd6@googlegroups.com>

On Friday, April 17, 2015 at 4:13:04 PM UTC-4, George Mpouras wrote:
> On 17/4/2015 10:34 =CE=BC=CE=BC, wrote:
> > And if it looks like 8776a653-bd56-97a4-65c3-6543a767656a  ?
> >
>=20
> its ok but then you have to change the spaces as
>=20
>=20
> $mask  =3D join '', map { "%-40s" } 0..$#names;
> printf STDOUT  "$mask\n"  ,@names;
> $mask  =3D join '', map {"%-30s%-10s"} 0..$#names;
>=20
>=20
>=20
> Are you sure you do not need a proper formatting tool as Rainer said=20
> using e.g the Template ?
> https://metacpan.org/pod/Template
> But this is a task you should try your self , as using this module have=
=20
> almost nothing to do with Perl or programming

this is works perfectly. One more question. Sorry to be annoying. i'm openi=
ng the end result in excel. Is there a way in this script that the entries =
would be considered 1 column ?

0157a353-fa74-4a32-ba4d-40c5623623ef  =20
1429283609                    0         =20
1429283608                    5       =20
 ...                        


------------------------------

Date: Fri, 17 Apr 2015 22:28:33 -0700
From: Jim Gibson <JimSGibson@gmail.com>
Subject: Re: format file
Message-Id: <170420152228335635%JimSGibson@gmail.com>

In article <e6d6d6c7-f173-463b-8e4b-955ebd5e1553@googlegroups.com>,
<lancerset@gmail.com> wrote:


> 
> this is works perfectly. One more question. Sorry to be annoying. i'm opening
> the end result in excel. Is there a way in this script that the entries would
> be considered 1 column

You can use Perl to write Excel spreadsheets directly. Use the
Excel::Writer::XLSX. The module is a little complex, but the
documentation is excellent.

-- 
Jim Gibson


------------------------------

Date: Sat, 18 Apr 2015 14:03:39 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: format file
Message-Id: <87618ty4es.fsf@doppelsaurus.mobileactivedefense.com>

Jim Gibson <JimSGibson@gmail.com> writes:
> In article <e6d6d6c7-f173-463b-8e4b-955ebd5e1553@googlegroups.com>,
> <lancerset@gmail.com> wrote:
>> this is works perfectly. One more question. Sorry to be annoying. i'm
>> opening the end result in excel. Is there a way in this script that
>> the entries would be considered 1 column
>
> You can use Perl to write Excel spreadsheets directly. Use the
> Excel::Writer::XLSX. The module is a little complex, but the
> documentation is excellent.

That's a serious overkill for such a simple task: Just do away with all
of the fancy text formatting and write a comma-separated ('CSV') file
Excel can then import.


------------------------------

Date: Sat, 18 Apr 2015 16:14:41 -0700
From: Jim Gibson <JimSGibson@gmail.com>
Subject: Re: format file
Message-Id: <180420151614415406%JimSGibson@gmail.com>

In article <87618ty4es.fsf@doppelsaurus.mobileactivedefense.com>,
Rainer Weikusat <rweikusat@mobileactivedefense.com> wrote:

> Jim Gibson <JimSGibson@gmail.com> writes:
> > In article <e6d6d6c7-f173-463b-8e4b-955ebd5e1553@googlegroups.com>,
> > <lancerset@gmail.com> wrote:
> >> this is works perfectly. One more question. Sorry to be annoying. i'm
> >> opening the end result in excel. Is there a way in this script that
> >> the entries would be considered 1 column
> >
> > You can use Perl to write Excel spreadsheets directly. Use the
> > Excel::Writer::XLSX. The module is a little complex, but the
> > documentation is excellent.
> 
> That's a serious overkill for such a simple task: Just do away with all
> of the fancy text formatting and write a comma-separated ('CSV') file
> Excel can then import.

Well, I disagree. It only takes a few statements to create a simple
Excel spreadsheet file. Once you have the basics down, you can start
adding niceties such as text alignment, bolding, italics, etc. 

Being able to generate spreadsheets directly from a Perl program is a
great feature. Doing the alternative: writing a CSV file, importing
that file into Excel, and saving as a spreadsheet, is more work. It is
better to spend the time learning how to generate a spreadsheet in one
step.

-- 
Jim Gibson


------------------------------

Date: Sun, 19 Apr 2015 10:13:27 +0200
From: "Peter J. Holzer" <hjp-usenet3@hjp.at>
Subject: Re: format file
Message-Id: <slrnmj6ot7.onp.hjp-usenet3@hrunkner.hjp.at>

On 2015-04-18 23:14, Jim Gibson <JimSGibson@gmail.com> wrote:
> In article <87618ty4es.fsf@doppelsaurus.mobileactivedefense.com>,
> Rainer Weikusat <rweikusat@mobileactivedefense.com> wrote:
>> Jim Gibson <JimSGibson@gmail.com> writes:
>> > You can use Perl to write Excel spreadsheets directly. Use the
>> > Excel::Writer::XLSX. The module is a little complex, but the
>> > documentation is excellent.
>> 
>> That's a serious overkill for such a simple task: Just do away with all
>> of the fancy text formatting and write a comma-separated ('CSV') file
>> Excel can then import.
>
> Well, I disagree. It only takes a few statements to create a simple
> Excel spreadsheet file. Once you have the basics down, you can start
> adding niceties such as text alignment, bolding, italics, etc. 
>
> Being able to generate spreadsheets directly from a Perl program is a
> great feature. Doing the alternative: writing a CSV file, importing
> that file into Excel, and saving as a spreadsheet, is more work.

It is also error-prone: When reading CSV files Excel uses different
field separators, number formats and date formats depending on the
Windows language settings (plus a number of heuristics). So a user
reading a CSV file with Excel may end up with garbage. Sometimes this is
immediately obvious (wrong field separator usually results in all the
data being crammed together in the first column), but sometimes it is
quite subtle and easy to miss. 

        hp


-- 
   _  | Peter J. Holzer    | Fluch der elektronischen Textverarbeitung:
|_|_) |                    | Man feilt solange an seinen Text um, bis
| |   | hjp@hjp.at         | die Satzbestandteile des Satzes nicht mehr
__/   | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel


------------------------------

Date: Sat, 18 Apr 2015 07:15:30 +0000 (UTC)
From: Martin Str|mberg <ams@ludd.ltu.se>
Subject: Uninitialized value, but it shouldn't be
Message-Id: <mgt0ag$aph$1@speranza.aioe.org>


This program:
#!/usr/bin/perl -w

use strict;
use warnings;


my @freq = (0 .. 257);

my $low;
my $high;

my $c = 128;

$low = 2*$c;
$high = 2*$c + 1;

my $i;
for $i (0 .. 255) {
    if( $i == 0 ) {
    	print "i==0\n";
    }
    if( $low < $freq[$i+1] ) {
    	print "last";
    }
}
if( $high <  $freq[$i+1] ) { # Line 26
    print "hello";
}


makes perl 5.10.1 on Debian 6 say:
i==0
Use of uninitialized value $i in addition (+) at ../bug.150418/bug.pl line 26.


Why? Evidently $i had the value 0 earlier and it shouldn't disappear as the
scope of $i includes line 26.



-- 
MartinS


------------------------------

Date: Sat, 18 Apr 2015 01:51:52 -0700 (PDT)
From: "C.DeRykus" <derykus@gmail.com>
Subject: Re: Uninitialized value, but it shouldn't be
Message-Id: <65a1c0ae-5797-4139-a59b-dc2d03f8c42b@googlegroups.com>

On Saturday, April 18, 2015 at 12:15:37 AM UTC-7, Martin Str|mberg wrote:
> This program:
> #!/usr/bin/perl -w
> 
> use strict;
> use warnings;
> 
> 
> my @freq = (0 .. 257);
> 
> my $low;
> my $high;
> 
> my $c = 128;
> 
> $low = 2*$c;
> $high = 2*$c + 1;
> 
> my $i;
> for $i (0 .. 255) {
>     if( $i == 0 ) {
>     	print "i==0\n";
>     }
>     if( $low < $freq[$i+1] ) {
>     	print "last";
>     }
> }
> if( $high <  $freq[$i+1] ) { # Line 26
>     print "hello";
> }
> 
> 
> makes perl 5.10.1 on Debian 6 say:
> i==0
> Use of uninitialized value $i in addition (+) at ../bug.150418/bug.pl line 26.
> 
> 
> Why? Evidently $i had the value 0 earlier and it shouldn't disappear as the
> scope of $i includes line 26.
> 


A new lexical variable will be created for the loop index if there's already one in scope. So, in your case, $i will revert to its previous undefined value
when the loop terminates.  That's the reason for the warning.

By the way, "foreach" and "for" are aliases.

perldoc perlsub:

 The "foreach" loop defaults to scoping its index variable dynamically
 in the manner of "local".  However, if the index variable is prefixed
 with the keyword "my", or if there is already a lexical by that name in
 scope, then a new lexical is created instead.  Thus in the loop

           for my $i (1, 2, 3) {
               some_function();
           }

the scope of $i extends to the end of the loop, but not beyond it...

-- 
Charles DeRykus
 




------------------------------

Date: Sat, 18 Apr 2015 11:25:52 +0200
From: gamo <gamo@telecable.es>
Subject: Re: Uninitialized value, but it shouldn't be
Message-Id: <mgt7v0$rb3$1@speranza.aioe.org>

El 18/04/15 a las 09:15, Martin Str|mberg escribió:
> This program:
> #!/usr/bin/perl -w
>
> use strict;
> use warnings;
>
>
> my @freq = (0 .. 257);

That's not what you probably want.

Try

my @freq = 0..257;


-- 
http://www.telecable.es/personales/gamo/
The generation of random numbers is too important to be left to chance


------------------------------

Date: Sat, 18 Apr 2015 12:30:23 +0000 (UTC)
From: Martin Str|mberg <ams@ludd.ltu.se>
Subject: Re: Uninitialized value, but it shouldn't be
Message-Id: <mgtios$mp7$1@speranza.aioe.org>

gamo <gamo@telecable.es> wrote:
> El 18/04/15 a las 09:15, Martin Str|mberg escribió:
> > my @freq = (0 .. 257);

> That's not what you probably want.

> Try

> my @freq = 0..257;


Possibly. However _I_ don't see the difference:
martin@hume:~$ perl -e 'my @freq = (0 .. 2); print join(":", @freq) ."\n";'
0:1:2
martin@hume:~$ perl -e 'my @freq = 0 .. 2; print join(":", @freq) ."\n";'
0:1:2


-- 
MartinS


------------------------------

Date: Sat, 18 Apr 2015 12:55:17 +0000 (UTC)
From: Martin Str|mberg <ams@luminous.ludd.ltu.se>
Subject: Re: Uninitialized value, but it shouldn't be
Message-Id: <mgtk7j$q6k$1@speranza.aioe.org>

C.DeRykus <derykus@gmail.com> wrote:
> On Saturday, April 18, 2015 at 12:15:37 AM UTC-7, Martin Str|mberg wrote:
 ...
> > my $i;
> > for $i (0 .. 255) {
> >     if( $i == 0 ) {
> >       print "i==0\n";
> >     }
> >     if( $low < $freq[$i+1] ) {
> >       print "last";
> >     }
> > }
> > if( $high <  $freq[$i+1] ) { # Line 26
> >     print "hello";
> > }
> > 
> > 
> > makes perl 5.10.1 on Debian 6 say:
> > i==0
> > Use of uninitialized value $i in addition (+) at ../bug.150418/bug.pl line 26.
> > 
> > 
> > Why? Evidently $i had the value 0 earlier and it shouldn't disappear as the
> > scope of $i includes line 26.
> > 


> perldoc perlsub:

>  The "foreach" loop defaults to scoping its index variable dynamically
>  in the manner of "local".  However, if the index variable is prefixed
>  with the keyword "my", or if there is already a lexical by that name in
>  scope, then a new lexical is created instead.  Thus in the loop

>            for my $i (1, 2, 3) {
>                some_function();
>            }

> the scope of $i extends to the end of the loop, but not beyond it...


Ok. That explains it. However here perl is amazingly counterintuitive.

foreach does one thing while for/while/until another. At least this is what I
think/learnt:

my $i = SOMETHING;
while( $i ) {...}

or

my $i;
for( $i=0; $i < 10; $i++) {...}

will not invent a local or lexical for just inside the while or for.


I was going to ask "why is the behaviour of for/foreach hidden in perldoc
erlsub", but I see similar wording in perldoc perlsyn.


Also, for and foreach may be synonyms, but the behaviour of for loop and
foreach loop is completely different in regard to the loop variable.

By saying so, you're implying same the behaviour is the same.


-- 
MartinS


------------------------------

Date: Sat, 18 Apr 2015 15:33:01 +0200
From: gamo <gamo@telecable.es>
Subject: Re: Uninitialized value, but it shouldn't be
Message-Id: <mgtmec$vsl$1@speranza.aioe.org>

El 18/04/15 a las 14:30, Martin Str|mberg escribió:
> gamo <gamo@telecable.es> wrote:
>> El 18/04/15 a las 09:15, Martin Str|mberg escribió:
>>> my @freq = (0 .. 257);
>
>> That's not what you probably want.
>
>> Try
>
>> my @freq = 0..257;
>
>
> Possibly. However _I_ don't see the difference:
> martin@hume:~$ perl -e 'my @freq = (0 .. 2); print join(":", @freq) ."\n";'
> 0:1:2
> martin@hume:~$ perl -e 'my @freq = 0 .. 2; print join(":", @freq) ."\n";'
> 0:1:2
>
>

I can reproduce it with 5.18.2 and the warning vanished if you
substitute

my $i;

by

my $i = 0;

which is, or must be, equivalent for a variable which is
after that used. So you could be right in that something
is wrong. Maybe it could be related with the exceptions
made with one letter variables. Maybe.

-- 
http://www.telecable.es/personales/gamo/
The generation of random numbers is too important to be left to chance


------------------------------

Date: Sat, 18 Apr 2015 14:35:43 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Uninitialized value, but it shouldn't be
Message-Id: <87a8y55zkg.fsf@doppelsaurus.mobileactivedefense.com>

Martin Str|mberg <ams@luminous.ludd.ltu.se> writes:
> C.DeRykus <derykus@gmail.com> wrote:
>> On Saturday, April 18, 2015 at 12:15:37 AM UTC-7, Martin Str|mberg wrote:
>> > my $i;
>> > for $i (0 .. 255) {
>> >     if( $i == 0 ) {
>> >       print "i==0\n";
>> >     }
>> >     if( $low < $freq[$i+1] ) {
>> >       print "last";
>> >     }
>> > }
>> > if( $high <  $freq[$i+1] ) { # Line 26
>> >     print "hello";
>> > }

[...]

>> perldoc perlsub:
>
>>  The "foreach" loop defaults to scoping its index variable dynamically
>>  in the manner of "local".  However, if the index variable is prefixed
>>  with the keyword "my", or if there is already a lexical by that name in
>>  scope, then a new lexical is created instead.  Thus in the loop
>
>>            for my $i (1, 2, 3) {
>>                some_function();
>>            }
>
>> the scope of $i extends to the end of the loop, but not beyond it...
>
> Ok. That explains it. However here perl is amazingly counterintuitive.
>
> foreach does one thing while for/while/until another. At least this is what I
> think/learnt:
>
> my $i = SOMETHING;
> while( $i ) {...}
>
> or
>
> my $i;
> for( $i=0; $i < 10; $i++) {...}
>
> will not invent a local or lexical for just inside the while or for.

A foreach loop would usually work with $_ (and there's usually no good
reason to change that) and since that's a 'global' variable, it's
implicitly localized to avoid trashing the previous value and to enable
nesting of foreach-loops.

Somewhat silly example illustrating this property:

----------
for (1 .. 10) {
    print("$_\n") for 0 .. $_;
    print("\n");
}
----------

While he other loops just provide a test for a condition and it's up to the
programmer to write code which causes the condition to change, the
foreach-for is a higher-level construct: A user is not supposed to care
about any details of loop variable management but for this to be
possible, the loop must not intefere with existing variables.

The easy way to avoid being surprised by this is just "don't care about
loop variable of foreach loops": Unless both the previous $_ and the
current value of the loop variable of a foreach loop are needed by the
code, use the default.


------------------------------

Date: Sat, 18 Apr 2015 14:37:57 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Uninitialized value, but it shouldn't be
Message-Id: <87618t5zgq.fsf@doppelsaurus.mobileactivedefense.com>

gamo <gamo@telecable.es> writes:
> El 18/04/15 a las 09:15, Martin Str|mberg escribió:
>> This program:
>> #!/usr/bin/perl -w
>>
>> use strict;
>> use warnings;
>>
>>
>> my @freq = (0 .. 257);
>
> That's not what you probably want.
>
> Try
>
> my @freq = 0..257;

The first expression has a superfluous pair of parentheses (-thesises?
parens?) but apart from that, I see no difference.

?


------------------------------

Date: Sat, 18 Apr 2015 14:41:15 +0100
From: Rainer Weikusat <rweikusat@mobileactivedefense.com>
Subject: Re: Uninitialized value, but it shouldn't be
Message-Id: <871tjh5zb8.fsf@doppelsaurus.mobileactivedefense.com>

gamo <gamo@telecable.es> writes:
> El 18/04/15 a las 14:30, Martin Str|mberg escribió:
>> gamo <gamo@telecable.es> wrote:
>>> El 18/04/15 a las 09:15, Martin Str|mberg escribió:
>>>> my @freq = (0 .. 257);
>>
>>> That's not what you probably want.
>>
>>> Try
>>
>>> my @freq = 0..257;
>>
>>
>> Possibly. However _I_ don't see the difference:
>> martin@hume:~$ perl -e 'my @freq = (0 .. 2); print join(":", @freq) ."\n";'
>> 0:1:2
>> martin@hume:~$ perl -e 'my @freq = 0 .. 2; print join(":", @freq) ."\n";'
>> 0:1:2
>>
>>
>
> I can reproduce it with 5.18.2 and the warning vanished if you
> substitute
>
> my $i;
>
> by
>
> my $i = 0;

In order to make the code work as was probably intended, this would need
to be

my $i = 255;


------------------------------

Date: Sat, 18 Apr 2015 15:59:04 +0200
From: gamo <gamo@telecable.es>
Subject: Re: Uninitialized value, but it shouldn't be
Message-Id: <mgtnv6$3h0$1@speranza.aioe.org>

El 18/04/15 a las 15:37, Rainer Weikusat escribió:
> gamo <gamo@telecable.es> writes:
>> El 18/04/15 a las 09:15, Martin Str|mberg escribió:
>>> This program:
>>> #!/usr/bin/perl -w
>>>
>>> use strict;
>>> use warnings;
>>>
>>>
>>> my @freq = (0 .. 257);
>>
>> That's not what you probably want.
>>
>> Try
>>
>> my @freq = 0..257;
>
> The first expression has a superfluous pair of parentheses (-thesises?
> parens?) but apart from that, I see no difference.
>
> ?
>
That's because I suspected (()) could be considered a list of lists,
and I don't know at what level 0..257 is substitued by (0,1, .. ,257)

False alarm.

-- 
http://www.telecable.es/personales/gamo/
The generation of random numbers is too important to be left to chance


------------------------------

Date: Sat, 18 Apr 2015 16:31:53 +0300
From: Eric Pozharski <whynot@pozharski.name>
Subject: Re: Uninitialized value, but it shouldn't be
Message-Id: <slrnmj4n69.qpd.whynot@orphan.zombinet>

with <mgt0ag$aph$1@speranza.aioe.org> Martin Str|mberg wrote:

*SKIP*
> my $i;
> for $i (0 .. 255) {
>     if( $i == 0 ) { print "i==0\n"; }
>     if( $low < $freq[$i+1] ) { print "last"; }
> }
> if( $high <  $freq[$i+1] ) { # Line 26
>     print "hello";
> }
*SKIP*
> Use of uninitialized value $i in addition (+) at ../bug.150418/bug.pl
> line 26.
> Why? Evidently $i had the value 0 earlier and it shouldn't disappear
> as the scope of $i includes line 26.

Surprise!  (Watch how location changes):

	% perl -MDevel::Peek -wle '
	  my $aa;
	  Dump $i;
	  foreach $i ( 0 .. 1 ) { Dump $i }         
	  Dump $i' 
	SV = NULL(0x0) at 0x956714c
	  REFCNT = 1
	  FLAGS = ()
	SV = IV(0x954d7e8) at 0x954d7ec
	  REFCNT = 1
	  FLAGS = (IOK,pIOK)
	  IV = 0
	SV = IV(0x954d7e8) at 0x954d7ec
	  REFCNT = 1
	  FLAGS = (IOK,pIOK)
	  IV = 1
	SV = NULL(0x0) at 0x956714c
	  REFCNT = 1
	  FLAGS = ()

-- 
Torvalds' goal for Linux is very simple: World Domination
Stallman's goal for GNU is even simpler: Freedom


------------------------------

Date: Sat, 18 Apr 2015 23:55:43 +0200
From: gamo <gamo@telecable.es>
Subject: Re: Uninitialized value, but it shouldn't be
Message-Id: <mgujsu$bpq$1@speranza.aioe.org>

El 18/04/15 a las 15:33, gamo escribió:
> El 18/04/15 a las 14:30, Martin Str|mberg escribió:
>> gamo <gamo@telecable.es> wrote:
>>> El 18/04/15 a las 09:15, Martin Str|mberg escribió:
>>>> my @freq = (0 .. 257);
>>
>>> That's not what you probably want.
>>
>>> Try
>>
>>> my @freq = 0..257;
>>
>>
>> Possibly. However _I_ don't see the difference:
>> martin@hume:~$ perl -e 'my @freq = (0 .. 2); print join(":", @freq)
>> ."\n";'
>> 0:1:2
>> martin@hume:~$ perl -e 'my @freq = 0 .. 2; print join(":", @freq) ."\n";'
>> 0:1:2
>>
>>
>
> I can reproduce it with 5.18.2 and the warning vanished if you
> substitute
>
> my $i;
>
> by
>
> my $i = 0;
>
> which is, or must be, equivalent for a variable which is
> after that used. So you could be right in that something
> is wrong. Maybe it could be related with the exceptions
> made with one letter variables. Maybe.
>

No, it's not one letter related. Seems a big hairy bug.

-- 
http://www.telecable.es/personales/gamo/
The generation of random numbers is too important to be left to chance


------------------------------

Date: Sun, 19 Apr 2015 00:27:31 +0200
From: gamo <gamo@telecable.es>
Subject: Re: Uninitialized value, but it shouldn't be
Message-Id: <mgulok$fnv$1@speranza.aioe.org>

El 18/04/15 a las 15:31, Eric Pozharski escribió:
> with <mgt0ag$aph$1@speranza.aioe.org> Martin Str|mberg wrote:
>
> *SKIP*
>> my $i;
>> for $i (0 .. 255) {
>>      if( $i == 0 ) { print "i==0\n"; }
>>      if( $low < $freq[$i+1] ) { print "last"; }
>> }
>> if( $high <  $freq[$i+1] ) { # Line 26
>>      print "hello";
>> }
> *SKIP*
>> Use of uninitialized value $i in addition (+) at ../bug.150418/bug.pl
>> line 26.
>> Why? Evidently $i had the value 0 earlier and it shouldn't disappear
>> as the scope of $i includes line 26.
>
> Surprise!  (Watch how location changes):
>
> 	% perl -MDevel::Peek -wle '
> 	  my $aa;
> 	  Dump $i;
> 	  foreach $i ( 0 .. 1 ) { Dump $i }
> 	  Dump $i'
> 	SV = NULL(0x0) at 0x956714c
> 	  REFCNT = 1
> 	  FLAGS = ()
> 	SV = IV(0x954d7e8) at 0x954d7ec
> 	  REFCNT = 1
> 	  FLAGS = (IOK,pIOK)
> 	  IV = 0
> 	SV = IV(0x954d7e8) at 0x954d7ec
> 	  REFCNT = 1
> 	  FLAGS = (IOK,pIOK)
> 	  IV = 1
> 	SV = NULL(0x0) at 0x956714c
> 	  REFCNT = 1
> 	  FLAGS = ()
>

Surprise! 1+1 = 3

$ perl -MDevel::Peek -Mstrict -wle '
our $i; Dump $i;
for ($i=0;$i<=1;$i++) { Dump $i; }
$i++;
Dump $i;'
SV = NULL(0x0) at 0x258fd90
   REFCNT = 1
   FLAGS = ()
SV = IV(0x258fd80) at 0x258fd90
   REFCNT = 1
   FLAGS = (IOK,pIOK)
   IV = 0
SV = IV(0x258fd80) at 0x258fd90
   REFCNT = 1
   FLAGS = (IOK,pIOK)
   IV = 1
SV = IV(0x258fd80) at 0x258fd90
   REFCNT = 1
   FLAGS = (IOK,pIOK)
   IV = 3


-- 
http://www.telecable.es/personales/gamo/
The generation of random numbers is too important to be left to chance


------------------------------

Date: Sun, 19 Apr 2015 10:03:37 +0200
From: "Peter J. Holzer" <hjp-usenet3@hjp.at>
Subject: Re: Uninitialized value, but it shouldn't be
Message-Id: <slrnmj6oap.onp.hjp-usenet3@hrunkner.hjp.at>

On 2015-04-18 12:55, Martin Str|mberg <ams@luminous.ludd.ltu.se> wrote:
> C.DeRykus <derykus@gmail.com> wrote:
>> perldoc perlsub:
>
>>  The "foreach" loop defaults to scoping its index variable dynamically
>>  in the manner of "local".  However, if the index variable is prefixed
>>  with the keyword "my", or if there is already a lexical by that name in
>>  scope, then a new lexical is created instead.  Thus in the loop
>
>>            for my $i (1, 2, 3) {
>>                some_function();
>>            }
>
>> the scope of $i extends to the end of the loop, but not beyond it...
>
>
> Ok. That explains it. However here perl is amazingly counterintuitive.

It shares this property with a lot of other languages, e.g. Modula-II
(value of the loop variable undefined after the loop) or Fortran
(assignment to a loop variable in the loop forbidden).

> foreach does one thing while for/while/until another. At least this is what I
> think/learnt:
>
> my $i = SOMETHING;
> while( $i ) {...}
>
> or
>
> my $i;
> for( $i=0; $i < 10; $i++) {...}
>
> will not invent a local or lexical for just inside the while or for.

A while loop doesn't have a designated loop variable. Neither has a
c-style for loop, which is just syntactic sugar for a while loop.

But a foreach loop does have a designated loop variable and it makes
sense to treat that specially. 

        hp


-- 
   _  | Peter J. Holzer    | Fluch der elektronischen Textverarbeitung:
|_|_) |                    | Man feilt solange an seinen Text um, bis
| |   | hjp@hjp.at         | die Satzbestandteile des Satzes nicht mehr
__/   | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel


------------------------------

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
Message-Id: <null>


Administrivia:

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

Back issues are available via anonymous ftp from
ftp://cil-www.oce.orst.edu/pub/perl/old-digests. 

#For other requests pertaining to the digest, send mail to
#perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
#sending perl questions to the -request address, I don't have time to
#answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V11 Issue 4417
***************************************


home help back first fref pref prev next nref lref last post