Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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

by JavaFan (Canon)
on Apr 07, 2009 at 16:52 UTC ( [id://756080]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^5: What is the difference between $array[1] and @array[1]?
by ikegami (Patriarch) on Apr 07, 2009 at 17:09 UTC
    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.

        No. Never used it. Never had a use for it. Never seen it used, not even in an example.

Re^5: What is the difference between $array[1] and @array[1]?
by Porculus (Hermit) on Apr 07, 2009 at 20:05 UTC
    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://756080]
help
Chatterbox?
and the web crawler heard nothing...

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