New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bug or feature? foreach (sort @array) {y/a-z/A-Z/;} # @array modified! #8029
Comments
From ping@bioservrp.es.dupont.comCreated by ping@bioservrp.es.dupont.com@arr = ('one', 'two', 'three', 'four'); foreach (sort @arr) foreach(@arr) The above code prints out " four one three two ONE TWO THREE FOUR". Perl Info
|
From @petdanceYes, this is correct. The loop variable is an alias. |
The RT System itself - Status changed from 'new' to 'open' |
@petdance - Status changed from 'open' to 'rejected' |
From prev a.r.ferreira@gmail.comThe described behavior changes whether "sort @arr" goes directly in @arr = ('one', 'two', 'three', 'four'); prints "a: ONE TWO THREE FOUR" and @arr = ('one', 'two', 'three', 'four'); prints "a: one two three four". This has to do with copy-on-assign? That seems like a (subtle) feature. Otherwise, it is a very old bug: |
From ping@micro3.es.dupont.comIf it's a feature, then it's not documented at all. Larry's Perl books do not Thanks for your time.
This communication is for use by the intended recipient and contains Francais Deutsch Italiano Espanol Portugues Japanese Chinese Korean http://www.DuPont.com/corp/email_disclaimer.html |
From ping@micro3.es.dupont.comBut shouldn't the alias be to the elements of the list returned by sort,
This communication is for use by the intended recipient and contains Francais Deutsch Italiano Espanol Portugues Japanese Chinese Korean http://www.DuPont.com/corp/email_disclaimer.html |
From @schwernOn Thu, Jul 21, 2005 at 10:20:22AM -0400, Shiping Zhang wrote:
That's what I'm thinking, that this is a sort/foreach optimization gone Its a bug. -- |
From @iabynOn Thu, Jul 21, 2005 at 01:15:39PM -0700, Michael G Schwern wrote:
I wouldn't expect map to maintain the alias, because map returns the $ perl587 -we '@a=qw(1 2 3); for (grep /\d/, @a) { $_++ } print "@a\n"' I don't think its a bug so much as undocumented behaviour. "fixing" it -- |
From @gbarrOn Thu, July 21, 2005 3:34 pm, Dave Mitchell said:
And reverse and any XS sub that directly returns any of its Graham. |
From @iabynOn Thu, Jul 21, 2005 at 04:22:19PM -0500, Graham Barr wrote:
Er, not the last one. subroutine returns make copies of the list -- |
From @ysthOn Thu, Jul 21, 2005 at 04:22:19PM -0500, Graham Barr wrote:
And repeat: $ perl -we'$x = "f"; for (($x) x 2) { print "in: $_\n"; $_ .= "o"; print "out: $_\n" }' |
From nospam@webdragon.netShiping Zhang wrote:
it's documented under perldoc perltrap as a discontinuance between perl4 it's also documented under perldoc perlsyn: "If any element of LIST is an lvalue, you can modify it by modifying VAR it's ALSO mentioned in perlfaq4: perldoc -q "strip blank space" |
From ping@micro3.es.dupont.comBy "not documented', I was refering to what is supposed to be returned by sort.
This communication is for use by the intended recipient and contains Francais Deutsch Italiano Espanol Portugues Japanese Chinese Korean http://www.DuPont.com/corp/email_disclaimer.html |
From ping@micro3.es.dupont.com
I think you only need to do copying at the end of sort, just treat it This communication is for use by the intended recipient and contains Francais Deutsch Italiano Espanol Portugues Japanese Chinese Korean http://www.DuPont.com/corp/email_disclaimer.html |
From nospam@webdragon.netShiping Zhang wrote:
I'm not so sure about that.. sort @a is just @a sorted. if I wanted a sorted copy of @a I'd do it implicitly: When I first looked at this question, I thoroughly discounted sort as being if I wanted a copy of @a I'd ask for it. Perhaps this needs to be documented |
From @schwernSo it sounds like the resolution for this bug is to document that sort -- |
From @schwernsort.pod.patch--- pod/perlfunc.pod 2005/07/24 02:57:16 1.2
+++ pod/perlfunc.pod 2005/07/24 02:58:42
@@ -4926,6 +4926,12 @@
When C<use locale> is in effect, C<sort LIST> sorts LIST according to the
current collation locale. See L<perllocale>.
+sort returns aliases into the original list, much as a for loop's
+index variable aliases the list elements. That is, modifying an
+element of a list returned by sort (for example, in a "foreach", "map"
+or "grep") actually modifies the element in the original list. This
+is usually something to be avoided when writing clear code.
+
Perl 5.6 and earlier used a quicksort algorithm to implement sort.
That algorithm was not stable, and I<could> go quadratic. (A I<stable> sort
preserves the input order of elements that compare equal. Although
|
From ping@micro3.es.dupont.comI agree.
This communication is for use by the intended recipient and contains Francais Deutsch Italiano Espanol Portugues Japanese Chinese Korean http://www.DuPont.com/corp/email_disclaimer.html |
From ping@micro3.es.dupont.com
But @a itself is not sorted, so you can't say you are receiving @a back.
The question here is not about the behavior of foreach, but that of Here are some words from http://perldoc.perl.org/functions/grep.html: This communication is for use by the intended recipient and contains Francais Deutsch Italiano Espanol Portugues Japanese Chinese Korean http://www.DuPont.com/corp/email_disclaimer.html |
From @rgsMichael G Schwern wrote:
Thanks, applied as change #25258. |
Migrated from rt.perl.org#36616 (status was 'rejected')
Searchable as RT36616$
The text was updated successfully, but these errors were encountered: