Interview Question: Array Referencing in Perl

Sample Question #240 (programming – Perl)
 
Explain the difference between the following in Perl:
$array[3]  vs.  $array->[3]
(Comment: I’ve heard that groups at big firms like Goldman, Morgan Stanley, UBS, etc. use Perl. Some other groups use Python, which I’ll ask of in the future)
Advertisements
This entry was posted in Sample Qs. Bookmark the permalink.

One Response to Interview Question: Array Referencing in Perl

  1. Brett says:

    ANSWER
     
    Because Perl’s basic data structure is all flat, references are the only way to build complex structures, which means references can be used in very tricky ways.
     
    This question is easy, though. In $array[3], "array" is the (symbolic) name of an array (@array) and $array[3] refers to the 4th element of this named array. In $array->[3], "array" is a hard reference to a (possibly anonymous) array, i.e., $array is the reference to this array, so $array->[3] is the 4th element of this array being referenced.
     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s