[24455] in Perl-Users-Digest
Perl-Users Digest, Issue: 6638 Volume: 10
daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Tue Jun 1 06:10:38 2004
Date: Tue, 1 Jun 2004 03:10: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 Tue, 1 Jun 2004 Volume: 10 Number: 6638
Today's topics:
Re: Reference to loop index in a closure <usenet@morrow.me.uk>
Re: Reference to loop index in a closure <uri@stemsystems.com>
Re: Reference to loop index in a closure <wksmith@optonline.net>
Re: Reference to loop index in a closure <uri@stemsystems.com>
Re: Reference to loop index in a closure <wksmith@optonline.net>
Re: Reference to loop index in a closure (Anno Siegel)
Re: Sorting and Ordering by date etc <thepotplants@yahoo.com>
Re: Sorting and Ordering by date etc <uri@stemsystems.com>
YAPC::2004 <dkm@cse.buffalo.edu>
Re: YAPC::2004 <matthew.garrish@sympatico.ca>
Re: YAPC::2004 <uri@stemsystems.com>
Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)
----------------------------------------------------------------------
Date: Tue, 1 Jun 2004 01:41:25 +0000 (UTC)
From: Ben Morrow <usenet@morrow.me.uk>
Subject: Re: Reference to loop index in a closure
Message-Id: <c9gms5$ari$1@wisteria.csv.warwick.ac.uk>
Quoth "gnari" <gnari@simnet.is>:
> "Bill Smith" <wksmith@optonline.net> wrote in message
> news:F7Ouc.34403$DC1.6921175@news4.srv.hcvlny.cv.net...
>
> > for (my $i = 0; $i < 3; $i++){
> > $ref1[$i] = sub{print "Case 1 = $i WRONG\n"};
> > }
> > &{$ref1[1]};
>
> i am no internal expert, but my perl sense tells me
> that when the index is made lexical here, it is equivalent to
>
>
>
> my $i;
> for ($i = 0; $i < 3; $i++){
> $ref1[$i] = sub{print "Case 1 = $i WRONG\n"};
> }
> }
>
> thus only one $i variable for the whole loop
To be exact, it's equivalent to:
{
my $i = 0;
while ($i < 3) {
...
}
continue {
$i++;
}
}
:)
Ben
--
If you put all the prophets, | You'd have so much more reason
Mystics and saints | Than ever was born
In one room together, | Out of all of the conflicts of time.
ben@morrow.me.uk The Levellers, 'Believers'
------------------------------
Date: Tue, 01 Jun 2004 02:08:35 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Reference to loop index in a closure
Message-Id: <x765ac6ndp.fsf@mail.sysarch.com>
>>>>> "BS" == Bill Smith <wksmith@optonline.net> writes:
BS> My "Case 1" creates an array of references to anonymous subroutines.
BS> Each subroutine is intended to print the value of the loop index ($i)
BS> when it (the subroutine) was created. One of the subroutines is
BS> executed to demonstrate that the wrong value is printed. (The index is
BS> never 3)
BS> "Case 2" is an experimental version which solves the problem by using
BS> another variable ($j) as the loop index
BS> and making $i a lexical variable within the scope of the loop.
BS> my @ref1;
BS> for (my $i = 0; $i < 3; $i++){
BS> $ref1[$i] = sub{print "Case 1 = $i WRONG\n"};
BS> }
BS> &{$ref1[1]};
the $i in the loop and closure are the SAME variable. so $i will end up
at 3 as it has to fail the loop. all of the closures will print three in
this case (try it) as they share the same variable as well.
BS> for (my $j = 0; $j < 3; $j++){
BS> my $i = $j;
BS> $ref1[$i] = sub{print "Case 2 = $i OK\n"};
BS> }
you copied the value before it hit 3 but even so, all the closures will
print 2. you never printed the closure earlier in the list and haven't
seen that bug yet.
BS> What is wrong with "Case 1"? I first encountered the problem in a Tk
BS> application and wrote this case as a aide to solving it. The successful
BS> "Case 2" was found by trial and error. Both appear to conform to
BS> section of "Programming Perl" on closures.
case 2 is not successful, you just didn't test it enough.
the variable being closed upon is visible to all closures created where
that variable is in scope. ATM, i can't think of a simple way to do what
you want. perhaps you have an XY problem and think you need to solve it
this way but really don't? what is the original problem?
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Tue, 01 Jun 2004 02:16:30 GMT
From: "Bill Smith" <wksmith@optonline.net>
Subject: Re: Reference to loop index in a closure
Message-Id: <2GRuc.1640$eU6.508482@news4.srv.hcvlny.cv.net>
"Bill Smith" <wksmith@optonline.net> wrote in message
news:F7Ouc.34403$DC1.6921175@news4.srv.hcvlny.cv.net...
--snip background info.
>
> use strict;
> use warnings;
>
> my @ref1;
>
> for (my $i = 0; $i < 3; $i++){
> $ref1[$i] = sub{print "Case 1 = $i WRONG\n"};
> }
> &{$ref1[1]};
>
>
> for (my $j = 0; $j < 3; $j++){
> my $i = $j;
> $ref1[$i] = sub{print "Case 2 = $i OK\n"};
> }
> &{$ref1[1]};
> __END__
>
> What is wrong with "Case 1"? I first encountered the problem in a Tk
> application and wrote this case as a aide to solving it. The
successful
> "Case 2" was found by trial and error. Both appear to conform to
> section of "Programming Perl" on closures.
>
Thanks guys, now I see my problem. I had recognized that the scope of
$i was different in Case 2, but failed to see why that made a
difference. Perl has to make a copy of a referenced variable when it
goes out of scope, not when its value changes.
Special thanks to Ben for pointing out the subtle advantage of the Perl
style loop statement. My Case 1 works fine with the new style loop.
Thanks again,
Bill
------------------------------
Date: Tue, 01 Jun 2004 03:05:56 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Reference to loop index in a closure
Message-Id: <x7r7t0565n.fsf@mail.sysarch.com>
>>>>> "BS" == Bill Smith <wksmith@optonline.net> writes:
BS> "Bill Smith" <wksmith@optonline.net> wrote in message
BS> news:F7Ouc.34403$DC1.6921175@news4.srv.hcvlny.cv.net...
BS> --snip background info.
>>
>> use strict;
>> use warnings;
>>
>> my @ref1;
>>
>> for (my $i = 0; $i < 3; $i++){
>> $ref1[$i] = sub{print "Case 1 = $i WRONG\n"};
>> }
>> &{$ref1[1]};
>>
>>
>> for (my $j = 0; $j < 3; $j++){
>> my $i = $j;
>> $ref1[$i] = sub{print "Case 2 = $i OK\n"};
>> }
>> &{$ref1[1]};
>> __END__
>>
BS> Thanks guys, now I see my problem. I had recognized that the scope of
BS> $i was different in Case 2, but failed to see why that made a
BS> difference. Perl has to make a copy of a referenced variable when it
BS> goes out of scope, not when its value changes.
BS> Special thanks to Ben for pointing out the subtle advantage of the Perl
BS> style loop statement. My Case 1 works fine with the new style loop.
did you properly test it as i asked? are all your closures printing
their correct values?
someone did post the correct way to do this with a closure generator
sub. this causes each closure to have its own private $i and they aren't
shared. this is because you enter the generator sub and get a fresh $i
each time. in your code you have a single $i and all the closures see
just it.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Tue, 01 Jun 2004 05:40:05 GMT
From: "Bill Smith" <wksmith@optonline.net>
Subject: Re: Reference to loop index in a closure
Message-Id: <VEUuc.2787$eU6.1079211@news4.srv.hcvlny.cv.net>
"Uri Guttman" <uri@stemsystems.com> wrote in message
news:x765ac6ndp.fsf@mail.sysarch.com...
> >>>>> "BS" == Bill Smith <wksmith@optonline.net> writes:
>
> BS> My "Case 1" creates an array of references to anonymous
subroutines.
> BS> Each subroutine is intended to print the value of the loop index
($i)
> BS> when it (the subroutine) was created. One of the subroutines
is
> BS> executed to demonstrate that the wrong value is printed. (The
index is
> BS> never 3)
>
> BS> "Case 2" is an experimental version which solves the problem by
using
> BS> another variable ($j) as the loop index
> BS> and making $i a lexical variable within the scope of the loop.
>
> BS> my @ref1;
>
> BS> for (my $i = 0; $i < 3; $i++){
> BS> $ref1[$i] = sub{print "Case 1 = $i WRONG\n"};
> BS> }
> BS> &{$ref1[1]};
>
> the $i in the loop and closure are the SAME variable. so $i will end
up
> at 3 as it has to fail the loop. all of the closures will print three
in
> this case (try it) as they share the same variable as well.
I tried all the closures for both cases before I posted my question.
I only included one of each in my example because
it seemed to demonstrate the problem.
>
> BS> for (my $j = 0; $j < 3; $j++){
> BS> my $i = $j;
> BS> $ref1[$i] = sub{print "Case 2 = $i OK\n"};
> BS> }
>
>
> you copied the value before it hit 3 but even so, all the closures
will
> print 2. you never printed the closure earlier in the list and haven't
> seen that bug yet.
I disagree. The code above prints "Case 2 = 1 OK"
If it had printed 2 it would not have been "OK".
The two closures, which are not tested in the example,
print the values 0 and 2 as required.
>
> BS> What is wrong with "Case 1"? I first encountered the problem in
a Tk
> BS> application and wrote this case as a aide to solving it. The
successful
> BS> "Case 2" was found by trial and error. Both appear to conform
to
> BS> section of "Programming Perl" on closures.
>
> case 2 is not successful, you just didn't test it enough.
>
See comment above.
> the variable being closed upon is visible to all closures created
where
> that variable is in scope. ATM, i can't think of a simple way to do
what
> you want. perhaps you have an XY problem and think you need to solve
it
> this way but really don't? what is the original problem?
>
You are right here. I have already found a better solution to the
original problem.
> uri
>
> --
> Uri Guttman ------ uri@stemsystems.com --------
http://www.stemsystems.com
> --Perl Consulting, Stem Development, Systems Architecture, Design and
Coding-
> Search or Offer Perl Jobs ----------------------------
http://jobs.perl.org
------------------------------
Date: 1 Jun 2004 09:04:26 GMT
From: anno4000@lublin.zrz.tu-berlin.de (Anno Siegel)
Subject: Re: Reference to loop index in a closure
Message-Id: <c9hgqq$obl$1@mamenchi.zrz.TU-Berlin.DE>
Uri Guttman <uri@stemsystems.com> wrote in comp.lang.perl.misc:
> >>>>> "BS" == Bill Smith <wksmith@optonline.net> writes:
>
> BS> My "Case 1" creates an array of references to anonymous subroutines.
> BS> Each subroutine is intended to print the value of the loop index ($i)
> BS> when it (the subroutine) was created. One of the subroutines is
> BS> executed to demonstrate that the wrong value is printed. (The index is
> BS> never 3)
>
> BS> "Case 2" is an experimental version which solves the problem by using
> BS> another variable ($j) as the loop index
> BS> and making $i a lexical variable within the scope of the loop.
>
> BS> my @ref1;
>
> BS> for (my $i = 0; $i < 3; $i++){
> BS> $ref1[$i] = sub{print "Case 1 = $i WRONG\n"};
> BS> }
> BS> &{$ref1[1]};
>
> the $i in the loop and closure are the SAME variable. so $i will end up
> at 3 as it has to fail the loop. all of the closures will print three in
> this case (try it) as they share the same variable as well.
>
> BS> for (my $j = 0; $j < 3; $j++){
> BS> my $i = $j;
> BS> $ref1[$i] = sub{print "Case 2 = $i OK\n"};
> BS> }
>
>
> you copied the value before it hit 3 but even so, all the closures will
> print 2. you never printed the closure earlier in the list and haven't
> seen that bug yet.
There is no bug. "my $i" happens before each closure is dispatched,
so it conserves the value $j had at that time.
$_->() for @ref1;
prints
Case 2 = 0 OK
Case 2 = 1 OK
Case 2 = 2 OK
just like it should.
Anno
------------------------------
Date: Tue, 1 Jun 2004 16:47:56 +1200
From: "ThePotPlants" <thepotplants@yahoo.com>
Subject: Re: Sorting and Ordering by date etc
Message-Id: <2UTuc.12851$XI4.449124@news.xtra.co.nz>
"ThePotPlants" <thepotplants@yahoo.com> wrote in message
news:47xuc.12357$XI4.434251@news.xtra.co.nz...
> I have a nasty set of text data that I want to sort into order, and remove
> duplicates from.
> This would only have taken 5 seconds in SQL, but I have no idea how to do
it
> in Perl.
>
> Data looks like so...
>
> -33.580333 162.601833 01/12/2003 00:01:09
> -33.579833 162.601667 01/12/2003 00:01:51
> -33.579167 162.601500 01/12/2003 00:03:09
> -33.578667 162.601333 01/12/2003 00:04:51
> -33.578667 162.601333 01/12/2003 00:05:09
I have cheated.
Imported into Access.
Write SQL. Group by, and order output.
Export to text file.
I feel dirty...
------------------------------
Date: Tue, 01 Jun 2004 05:01:32 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: Sorting and Ordering by date etc
Message-Id: <x765ab6fdf.fsf@mail.sysarch.com>
>>>>> "T" == ThePotPlants <thepotplants@yahoo.com> writes:
T> I have cheated.
T> Imported into Access.
T> Write SQL. Group by, and order output.
T> Export to text file.
T> I feel dirty...
please go wash your hands and branes. now!
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
Date: Mon, 31 May 2004 23:44:20 -0400
From: Daniel K Magnuszewski <dkm@cse.buffalo.edu>
Subject: YAPC::2004
Message-Id: <Pine.SOL.4.56.0405312342080.24448@gagarin.cse.buffalo.edu>
The Buffalo Perl Mongers are proud to announce that Buffalo is hosting
Yet Another Perl Conference (YAPC) at UB this summer, June 16-18, 2004.
YAPC is an inexpensive ($85), grassroots conference on the Perl
programming language aimed at beginners and experts alike, organized by
Yet Another Society, Inc., a non-profit corporation.
Details about the conference, including links to past conferences, are
available at the web site at: <http://yapc.org/America>. If you'd just
like to attend, registration is currently open. The fee is only $85 for
three full days.
If you're going to be in Buffalo this summer, consider setting aside
some time to attend YAPC.
------------------------------
Date: Tue, 1 Jun 2004 00:04:55 -0400
From: "Matt Garrish" <matthew.garrish@sympatico.ca>
Subject: Re: YAPC::2004
Message-Id: <yfTuc.14699$Hn.478611@news20.bellglobal.com>
"Daniel K Magnuszewski" <dkm@cse.buffalo.edu> wrote in message
news:Pine.SOL.4.56.0405312342080.24448@gagarin.cse.buffalo.edu...
>
> Details about the conference, including links to past conferences, are
> available at the web site at: <http://yapc.org/America>. If you'd just
> like to attend, registration is currently open. The fee is only $85 for
> three full days.
>
Price of gas to drive down from Toronto - $20
Price of entrance to YAPC 2004 - $85
Chance to heckle Uri - priceless!
If only I had more time... ; )
Matt
------------------------------
Date: Tue, 01 Jun 2004 05:00:51 GMT
From: Uri Guttman <uri@stemsystems.com>
Subject: Re: YAPC::2004
Message-Id: <x78yf76fek.fsf@mail.sysarch.com>
>>>>> "MG" == Matt Garrish <matthew.garrish@sympatico.ca> writes:
MG> "Daniel K Magnuszewski" <dkm@cse.buffalo.edu> wrote in message
MG> news:Pine.SOL.4.56.0405312342080.24448@gagarin.cse.buffalo.edu...
>>
>> Details about the conference, including links to past conferences, are
>> available at the web site at: <http://yapc.org/America>. If you'd just
>> like to attend, registration is currently open. The fee is only $85 for
>> three full days.
>>
MG> Price of gas to drive down from Toronto - $20
MG> Price of entrance to YAPC 2004 - $85
MG> Chance to heckle Uri - priceless!
MG> If only I had more time... ; )
ahh, you couldn't heckle your way out of a bag made by a php weenie! :)
and you will miss the chance to see harry potter on imax (and heckle
too) for free! see yapc.kwiki.org for more of the social stuff at
yapc::buffalo.
uri
--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
------------------------------
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:
#The Perl-Users Digest is a retransmission of the USENET newsgroup
#comp.lang.perl.misc. For subscription or unsubscription requests, send
#the single line:
#
# subscribe perl-users
#or:
# unsubscribe perl-users
#
#to almanac@ruby.oce.orst.edu.
NOTE: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice.
To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.
#To request back copies (available for a week or so), send your request
#to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
#where x is the volume number and y is the issue number.
#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 V10 Issue 6638
***************************************