Re: Is this a bug, or expected behavior?
by Fletch (Bishop) on Mar 17, 2006 at 15:17 UTC
|
Nope. .. in scalar context is a flip-flop operator. A constant expression on either side is compared against $.; since you've not read any lines of input $. does not equal 1, so it's evaluating to a false value 0.
See perlop which explains all this in detail.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
Ah, right. Okay, so I can see why scalar(1..4) is ''; now my question is, why is 1..4 getting a scalar context above when I assign to $b? Shouldn't [ $a->[1..4] ] cause 1..4 to be evaluated in list context?
--
@/=map{[/./g]}qw/.h_nJ Xapou cets krht ele_ r_ra/;
map{y/X_/\n /;print}map{pop@$_}@/for@/
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
|
|
|
|
$ cat wa
sub ctx {
my $w = wantarray;
if( defined $w ) {
print $w ? "array\n" : "scalar\n";
} else { print "void\n" }
1;
}
my $a = [ qw( a b c ) ];
$a->[ ctx( ) ];
@{$a}[ ctx( ) ];
$ perl wa
scalar
array
| [reply] [Watch: Dir/Any] [d/l] |
|
The [1..4] in list context is used with slices -- it doesn't create a list context on its own. Because you ask for $a->, you're telling Perl to dereference $a to a single element, not a slice, thus 1..4 is interpreted in scalar context.
In the case that works, you're dereferencing $a to an array, and then taking a slice of that -- and because you're slicing, 1..4 gets interpreted in list context.
$a->[1..4]; # not a slice
@{$a}[1..4]; # slice
-xdg
Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
Unlike @{$a}[1..4], $a->[1..4] is not a array slice. This table should help you.
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Is this a bug, or expected behavior?
by BrowserUk (Patriarch) on Mar 17, 2006 at 15:38 UTC
|
[0] Perl> $a = [ qw(mary had a little lamb with mint jelly) ];;
[0] Perl> $b = [ @{ $a }[ 1 .. 4 ] ];;
[0] Perl> print @$b;;
had a little lamb
I'm surprised that you didn't get a warning or two along the lines of:
Using an array as a reference is deprecated at ...
Argument "" isn't numeric in array element at ...
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [Watch: Dir/Any] [d/l] [select] |
Re: Is this a bug, or expected behavior?
by QM (Parson) on Mar 17, 2006 at 15:19 UTC
|
Tugging at the loose threads some more...
my $b = [ $a->[1,2,3,4] ];
gives a reference to an array with one element, undef.
I'm guessing these are both edge cases in the dereference mechanism (->). Someone with more expertise than myself will have to look at the optree for these.
-QM
--
Quantum Mechanics: The dreams stuff is made of
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
I would expect $a->[1, 2, 3, 4] to be the same as $a->[4], because the index is being evaluated in a scalar context, which means the comma is a "comma operator" (evaluate left expression, discard it, evaluate right expression). Correct?
| [reply] [Watch: Dir/Any] [d/l] [select] |
|
| [reply] [Watch: Dir/Any] |
Re: Is this a bug, or expected behavior?
by timos (Beadle) on Mar 17, 2006 at 15:24 UTC
|
if (1 .. 2) {print} and for (1 .. 2) {print} -- the same dots but different meaning :-) Regards, Timo | [reply] [Watch: Dir/Any] |
Re: Is this a bug, or expected behavior?
by rinceWind (Monsignor) on Mar 17, 2006 at 15:45 UTC
|
To get a slice, you need to use the @ sigil on an array, and the subscript is evaluated in list context. Case $c is the correct way to do this.
Case $b doesn't parse as a slice, but a single element dereference owing to the arrow. This forces scalar context on the subscript expression, turning your .. range operator into a flipflop operator, returning false (which is 0 in numeric context), hence the first element of @$a. In certain circumstances depending on the surrounding code, it could give you the second element, i.e. 'had'.
--
Oh Lord, won’t you burn me a Knoppix CD ?
My friends all rate Windows, I must disagree.
Your powers of persuasion will set them all free,
So oh Lord, won’t you burn me a Knoppix CD ? (Missquoting Janis Joplin)
| [reply] [Watch: Dir/Any] |
Re: Is this a bug, or expected behavior?
by GrandFather (Saint) on Mar 17, 2006 at 18:21 UTC
|
Heh, your basic problem is well answered. However, this was exactly the context that stimulated my meditation about the scalar range operator - you may be interested in reading Flipin good, or a total flop?.
DWIM is Perl's answer to Gödel
| [reply] [Watch: Dir/Any] |