Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

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

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


in reply to Re^2: What is the difference between $array[1] and @array[1]?
in thread What is the difference between $array[1] and @array[1]?

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.

Replies are listed 'Best First'.
Re^4: What is the difference between $array[1] and @array[1]?
by JavaFan (Canon) on Apr 07, 2009 at 16:52 UTC
    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.
      I don't see any use for @a[4]. I'd want to "fetch element at index 4", but @a[4] reads as "fetch elements at indexes 4". I don't have any problems with it warning.

      (Syntax and/or semantics are different in Perl6. This only applies to Perl5.)

        I do. It makes the one element list an exception (with an exception to the exception if said list has a trailing comma).
        @a[1,2,3,4]; # Ok. @a[1,2,3]; # Ok. @a[1,2]; # Ok. @a[1]; # Warning. @a[1,]; # Good boy, you know the secret handshake.
        You don't have a problem with that? I think it's a pointless irregularity.
      The issue is, what do you think when you see @a[4]?

      I think, "The person who wrote this can't be very familiar with Perl."

      If I myself were not familiar with Perl, I would probably think the something similar to the person who started this thread: "Huh? I thought you had to say $a[4]! What strange magic is this?"

      Maybe it would have been better if the language wasn't this way, but it is, and it's best to use it idiomatically and to urge others to do so as well: if you don't, experienced hackers may mistake you for a beginner, and beginners will find your code confusing.

        I think, "The person who wrote this can't be very familiar with Perl."
        Well, it's more a "nanananana, you expected regularity! Fooled you! Should have used a secret handshake!" unfamiliarity.
        What strange magic is this?
        Why would anyone expect single element lists to have strange magic?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2025-01-15 05:39 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.