Re: Scalar value @scores_sorted[$i] better written as $scores_sorted[$i]
by Anonymous Monk on Jan 03, 2011 at 02:49 UTC
|
For what it's worth, Perlish ways to do that include
for my $i (0..$#scores) { print $scores[$i], "\n"; }
for my $score (@scores) { print $score, "\n"; }
Or even print for @scores; (after perl -l or $/ = "\n";; or just say for @scores; in Perl 5.10 or later after use feature 'say';).
It may not be worth mentioning, but the test $i <= $#scores should probably be considered an excessive use of the special variable $#foo. Because of its complexity (it's an lvalue, among other things), there can be performance implications in repeatedly accessing or setting it. In this case it's moot, since the reasonable way to access the array's elements is to iterate over each element or each index (the latter results in it being used only wants). But don't avoid a sensible construct out of hand without considering benchmarks and input from Monks past and present -- it's likely niggling points like this will have no significance in a larger program that hasn't been profiled. | [reply] [d/l] [select] |
Re: Scalar value @scores_sorted[$i] better written as $scores_sorted[$i]
by ikegami (Patriarch) on Jan 03, 2011 at 04:17 UTC
|
$foo # Scalar
$foo[$i] # Array element, a scalar
$foo{$k} # Hash element, a scalar
@foo # Array contents, a list of scalar values
@foo[$i, $j] # Array slice, a list of scalar values
@foo{$k1, $k2} # Hash slice, a list of scalar values
%foo # Hash contents, a list of key-value pairs
$foo[EXPR] and @foo[EXPR] differ in the context in which EXPR is evaluated, and they differ in the number of scalars they can return. | [reply] [d/l] [select] |
|
|
The sigil to use is the type being returned
Actually, it isn't. Here are some counter examples:
@foo # Number of elements of the array.
@foo # A reference to the array.
@foo[$i, $j] # Last element of slice.
@foo{$k1, $k2} # Last element of slice.
%foo # A reference to the hash.
%foo # True/false.
%foo # String with ratio of filled buckets and buckets.
The sigil can influence what is returned, but foremost, it's the context that will determine what is returned. In particular, in scalar context, a scalar will always be returned. | [reply] [d/l] |
|
|
The counter examples fall into two categories: scalar context, where nothing but a scalar can be returned, and where rules are used to make the variable an lvalue.
| [reply] |
|
|
Re: Scalar value @scores_sorted[$i] better written as $scores_sorted[$i]
by Anonyrnous Monk (Hermit) on Jan 03, 2011 at 02:38 UTC
|
$ echo 'Scalar value @scores[$i] better written as $scores[$i]' | spla
+in
Scalar value @scores[$i] better written as $scores[$i] (#1)
(W syntax) You've used an array slice (indicated by @) to select a
single element of an array. Generally it's better to ask for a sc
+alar
value (indicated by $). The difference is that $foo[&bar] always
behaves like a scalar, both when assigning to it and when evaluati
+ng its
argument, while @foo[&bar] behaves like a list when you assign to
+it,
and provides a list context to its subscript, which can do weird t
+hings
if you're expecting only one subscript.
On the other hand, if you were actually hoping to treat the array
element as a list, you need to look into how references work, beca
+use
Perl will not magically convert between scalars and lists for you.
+ See
perlref.
| [reply] [d/l] |
Re: Scalar value @scores_sorted[$i] better written as $scores_sorted[$i]
by NetWallah (Canon) on Jan 03, 2011 at 02:34 UTC
|
Your version is canonical in perl6.
Perl 5 is stricter - it insists on Scalars being represented by the "$" sigil - in your case, printing ONE score item is a scalar, so perl (5) expects a "$".
Syntactic sugar causes cancer of the semicolon. --Alan Perlis
| [reply] |
|
|
Perl 5 is stricter - it insists on Scalars being represented by the "$" sigil - in your case, printing ONE score item is a scalar, so perl (5) expects a "$".
Partially right. In this case, a one element list is printed.
I find the warning highly annoying. It's a case where Perl knows what I mean, does what I mean, and still thinks it's Python, insisting in one true way of doing things. Despite it being the syntax of the future.
| [reply] |
Re: Scalar value @scores_sorted[$i] better written as $scores_sorted[$i]
by Anonymous Monk on Jan 03, 2011 at 05:16 UTC
|
Because you are printing a single value from the array @scores_sorted, that value is by definition a scalar.
For any scalar value (even when part of a bigger structure) Perl expects to see a leading dollar sign. So the first element of array @foo is $foo[0], not @foo[0].
-Tom Williams | [reply] |
Re: Scalar value @scores_sorted[$i] better written as $scores_sorted[$i]
by Anonymous Monk on Jan 03, 2011 at 09:40 UTC
|
Use :
print $scores$i; print "\n";
| [reply] |
|
|
With foreach is faster
foreach (@scores ){
print $_."\n";
}
| [reply] |