Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Scalar value @scores_sorted[$i] better written as $scores_sorted[$i]

by filesurfer (Novice)
on Jan 03, 2011 at 02:24 UTC ( #880111=perlquestion: print w/replies, xml ) Need Help??
filesurfer has asked for the wisdom of the Perl Monks concerning the following question:

I am learning perl. I just read perlintro and I'm typing up some learning snippets to see what happens when I type in various cases. now my code is:
for (my $i = 0; $i <= $#scores; $i++) { print @scores[$i]; print "\n"; }
Then it tells me:
Scalar value @scores[$i] better written as $scores[$i] at ./how to pri +nt a char.pl line 18. Scalar value @scores_sorted[$i] better written as $scores_sorted[$i] a +t ./how to print a char.pl line 24.
I thought that my version was canonical versus what the Perl interpeter was advising me? Can more experienced monks please explain this to me.

Replies are listed 'Best First'.
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.

Re: Scalar value @scores_sorted[$i] better written as $scores_sorted[$i]
by ikegami (Pope) on Jan 03, 2011 at 04:17 UTC

    The sigil to use is the type being returned.

    $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.

      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.
        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.
Re: Scalar value @scores_sorted[$i] better written as $scores_sorted[$i]
by Anonyrnous Monk (Hermit) on Jan 03, 2011 at 02:38 UTC

    Let diagnostics explain:

    $ 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.
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

      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.

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
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";
      With foreach is faster foreach (@scores ){ print $_."\n"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://880111]
Approved by ww
Front-paged by ww
help
Chatterbox?
and the leaves swirl about...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2018-07-21 17:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (449 votes). Check out past polls.

    Notices?