Posted By

dvdrtrgn on 08/19/08


Tagged

php arrow dereferencing mystical symbols


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

dvdrtrgn


The equals greater than operands


 / Published in: HTML
 

URL: http://groups.google.com/group/comp.infosystems.www.authoring.cgi

  1. <pre>
  2. Tiffany S. (NiceTiff...@webtv.net) wrote:
  3.  
  4. : What do the symbols => and -> mean? I can find them used in scripts,
  5. : but I can't find them explained in either books or online.
  6.  
  7. Without even knowing the language it's hard to give a correct answer.
  8.  
  9. But I'll guess your using perl.
  10.  
  11. First, if indeed perl is the language, then the answer most certainly is
  12. available online, you just need to know what to read.
  13.  
  14. If you have perl installed then type
  15. perldoc perlsyn
  16. perldoc perlop
  17.  
  18. If you don't have perl installed then type the above into google instead,
  19. or google something like "perl" "operators"
  20.  
  21. Anyway, in perl, => is a special type of comma. It's used like a comma
  22. but the thing on the left is automatically quoted (basically).
  23.  
  24. so
  25. ("one","two") is similar to (one=>"two")
  26.  
  27. And -> means "points to". It's taken from C notation and has a similarish
  28. meaning. If, instead of a plain old variable that contains a value, you
  29. instead have a variable that _references_ another variable, then you can't
  30. get the value directly because it isn't kept "in" the variable. Instead
  31. the variable "points to" where the value is kept. Read the doc for
  32. details.
  33.  
  34. so you will see things like
  35.  
  36. $x[0] # first element of an array called "x"
  37.  
  38. $x->[0] # first element of an array, but the array is _not_ called
  39. # "x". Instead the array is kept somewhere else, and
  40. # the variable "x" _points to_ where the array is being
  41. # kept.
  42.  
  43. <hr>
  44.  
  45.  
  46. Tiffany S. wrote:
  47. > What is the explanation of the => and the -> operands? I've run
  48. > across them, but can't find anything about them in tutorials or
  49. > reference manuals.
  50.  
  51. => can be used in place of a comma, and is commonly used to separate
  52. key/value pairs when creating a hash.
  53.  
  54. my %hash = (dog, 'bark',
  55. cat, 'meow',
  56. cow, 'moo',
  57. pig, 'oink'
  58. );
  59.  
  60. can be written as:
  61.  
  62. my %hash = (dog => 'bark',
  63. cat => 'meow',
  64. cow => 'moo',
  65. pig => 'oink'
  66. );
  67.  
  68. The => simply makes it easier to read.
  69.  
  70. If, instead, I had written:
  71.  
  72. my $hash = {dog => 'bark',
  73. cat => 'meow',
  74. cow => 'moo',
  75. pig => 'oink'
  76. };
  77.  
  78. (note the curly brackets instead of parentheses) then $hash would
  79. contain a reference to a hash. I would have to dereference the hash to
  80. access any of the values for a given key.
  81.  
  82. I could
  83.  
  84. print $$hash{dog};
  85.  
  86. or I could
  87.  
  88. print $hash->{dog};
  89.  
  90. If you are not familiar with references, look here:
  91. http://perldoc.perl.org/perlreftut.html
  92. </pre>

Report this snippet  

You need to login to post a comment.