fizbin has asked for the wisdom of the Perl Monks concerning the following question:
While working on an algorithm for finding polynomials to fit a bunch of points (which you can see on my scratchpad), I tripped across this little syntax quirk and wondered this behavior is expected, or possibly a bug in my perl (version 5.8.7, the stock cygwin build).
Namely, I found that this code:
produces this output:use strict; my $a = [ qw(mary had a little lamb with mint jelly) ]; my $b = [ $a->[1..4] ]; my $c = [ @{$a}[1..4] ]; print "a is [ @$a ]\n"; print "b is [ @$b ]\n"; print "c is [ @$c ]\n";
a is [ mary had a little lamb with mint jelly ] b is [ mary ] c is [ had a little lamb ]
That is, $b is equal to [ $a->[0] ]. I cannot imagine how perl's syntax rules got there. Apparently, 1..4 is being evaluated in scalar context (despite the expression $a->[1..4] clearly being evaluated in a list context), and the result of scalar(1..4) is numerically equal to 0. This struck me as bizarre; I would have thought scalar(1..4) would be 4, not ''.
Is anyone else startled by this behavior?
@/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/; map{y/X_/\n /;print}map{pop@$_}@/for@/
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Is this a bug, or expected behavior?
by Fletch (Bishop) on Mar 17, 2006 at 15:17 UTC | |
by fizbin (Chaplain) on Mar 17, 2006 at 15:24 UTC | |
by merlyn (Sage) on Mar 17, 2006 at 15:39 UTC | |
by fizbin (Chaplain) on Mar 17, 2006 at 15:47 UTC | |
by merlyn (Sage) on Mar 17, 2006 at 16:16 UTC | |
by Fletch (Bishop) on Mar 17, 2006 at 16:13 UTC | |
| |
by Fletch (Bishop) on Mar 17, 2006 at 15:37 UTC | |
by xdg (Monsignor) on Mar 17, 2006 at 15:51 UTC | |
by ikegami (Patriarch) on Mar 17, 2006 at 16:51 UTC | |
Re: Is this a bug, or expected behavior?
by BrowserUk (Patriarch) on Mar 17, 2006 at 15:38 UTC | |
Re: Is this a bug, or expected behavior?
by QM (Parson) on Mar 17, 2006 at 15:19 UTC | |
by merlyn (Sage) on Mar 17, 2006 at 16:21 UTC | |
by QM (Parson) on Mar 19, 2006 at 17:25 UTC | |
Re: Is this a bug, or expected behavior?
by timos (Beadle) on Mar 17, 2006 at 15:24 UTC | |
Re: Is this a bug, or expected behavior?
by rinceWind (Monsignor) on Mar 17, 2006 at 15:45 UTC | |
Re: Is this a bug, or expected behavior?
by GrandFather (Saint) on Mar 17, 2006 at 18:21 UTC |