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 sundialsvc4 (Abbot)
on Apr 07, 2009 at 16:32 UTC ( [id://756072]=note: print w/replies, xml ) Need Help??


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

As you have seen (and may know...) the preceding character, "$" or "@", defines the context in which the subsequent phrase will be interpreted.

Context is a concept that is fairly unique to Perl, AFAIK.

Replies are listed 'Best First'.
Re^2: What is the difference between $array[1] and @array[1]?
by salazar (Scribe) on Apr 08, 2009 at 11:34 UTC
    I can't speak for anything other than PHP and JavaScript (I know, I know... extremely ugly languages), but they both use the same symbol to represent what we know in Perl as scalars, lists, and hashes.

    I also could add that this allows for extremely messy and poor practices in code. I've seen this many times:
    <?php $foo = 'bar'; $foo['bar'] = 'ding'; ?>

    I'm assuming (as I've never looked at the PHP source code) that there are actually two separate variables here, one an array and one a scalar. But then, what happens when you try to do "if ($foo)"?

    Naming two variables the same, @foo and $foo, isn't the best practice in Perl, but at least it's possible ;) .
      It used to be that in Perl, a '$' introduced a scalar value/variable (1 value), a '@' a list value/array (0, 1 or more values) and a '%' a hash variable. Life was simple, and sigils easy to explain.

      Then we got perl5 and objects, and expressions like $obj->method, where the sigil is '$' even if the method is going to return multiple values. Life no longer is simple, and people got the feeling sigils are wrong and Perl6 will change how sigils are used.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in 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.