Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: What is the difference between $array[1] and @array[1]?

by ikegami (Patriarch)
on Apr 07, 2009 at 14:32 UTC ( [id://756042]=note: print w/replies, xml ) Need Help??


in reply to What is the difference between $array[1] and @array[1]?

In practice, very little except for the warning.
>perl -we"@x = @x[1]" Scalar value @x[1] better written as $x[1] at -e line 1.

Use $ unless you mean to fetch more than one element from the array at time.

my $ele = $array[1]; # Array index my @eles = @array[1,2,3]; # Array slice

Think of it this way: The sigil indicates the kind of value being returned. If you want a scalar, use the scalar sigial ($). If you want an list, use the array sigil (@). While Perl won't care in most situations, it'll signal your intent to the next person to read your code.

Replies are listed 'Best First'.
Re^2: What is the difference between $array[1] and @array[1]?
by JavaFan (Canon) on Apr 07, 2009 at 16:21 UTC
    If you want a scalar, use the scalar sigial ($). If you want an list, use the array sigil (@). While Perl won't care in most situations, it'll signal your intent to the next person to read your code.

    Does that mean you disagree with the warning? Or are you like the ancient Greek who didn't consider 1 not to be a proper number, and you don't consider one-element lists to be proper lists?

    I appreciate the idea that a leading $ signals you want a single element, and a leading @ signals you want a slice. But sometimes, the slice one wants is just one element.

      Does that mean you disagree with the warning?

      It's meant to enforce style, but I agree with the style being enforced, and it can detect typos (refactoros?).

      Or are you like the ancient Greek who didn't consider 1 not to be a proper number, and you don't consider one-element lists to be proper lists?

      Do you honestly think in terms of lists when you do $a[4]? I don't.

      But sometimes, the slice one wants is just one element.

      Of course. I didn't mean to imply otherwise. Note that Perl doesn't warn when you don't use a constant.

      $ perl -wle'my @a = qw( a b c ); my @i = 2; print @a[@i]' c

      In fact, using $ wouldn't work in that situation.

      $ perl -wle'my @a = qw( a b c ); my @i = 2; print $a[@i]' b

      I can't figure out what you think I was saying, but hopefully this clears it up.

        Do you honestly think in terms of lists when you do $a[4]? I don't.
        Neither do I. But that's not the point. The issue is, what do you think when you see @a[4]? The sigil makes me think "list". It turns out to be a one element list. Good. Nothing wrong with that. IMO.

        But Perl warns. It thinks there might be a problem.

        I can't figure out what you think I was saying, but hopefully this clears it up.
        I cannot figure out whether you think that it's good that Perl warns about @a[1] or that you think it shouldn't warn, but it gives the programmer the freedom to signal the intent to use a list.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://756042]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2025-01-15 06:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which URL do you most often use to access this site?












    Results (46 votes). Check out past polls.