Arrays and Hashes



Is this correct? The type of parenthesis etc?
Guy

@x; Entire Array
%x; Entire Hash

$x[0]; One item of Array
%x{key}; One item of Hash

$r=\@x; Referencing Array variable
$r=\%x; Referencing Hash variable

@$r; De-referecing entire array
De-referecing entire hash ???

$$r[0]; Referencing one array item
$$r{key}; Referencing one hash item

(v1,v2); Anonymous Array (depending on context)
(k1,v1,k2,v2); Anonymous Hash (depending on context)

[v1,v2]; Reference to Anonymous Array
{k1,v1,k2,v2}; Reference to Anonymous Hash

$a=(1,2,3)[0]; Slice of a list
@x=(1,2,3)[0,1]; Slices of a list
($a,$b)=(1,2,3)[0,1]; Slices of a list
@y=@x[0,1]; Slices of array

@x=@y{k1,k2}; Slices of hash (use @ not % because partial hash)
@x=@y{@keys}; Slices of hash (use @ not % because partial hash)

@x{@keys}=@values; Assigning to specific or all keys of hash

$y[0]=\@x; Creating Array of Array (2D Array)
$y[0][0]; Accessing item

$y[0]=\%x; Creating Array of Hash
$y[0]{key}; Accessing item

$y{key}=%x; Creating Hash of Hash
$y{key}{key2}; Accessing item


.



Relevant Pages

  • Re: Hash pointing to reference of itself!
    ... Is this possible without referencing outside the hash ... Forgive my lack of understanding of ruby internals. ... def initialize ...
    (comp.lang.ruby)
  • Re: Sytem hook for instance names?
    ... > which object I'm referencing). ... If the kernel has a hash of references, ... a namespace and namespaces are dictionaries. ... through associations in Smalltalk dictionaries (although this might be ...
    (comp.lang.ruby)
  • Re: Arrays and Hashes
    ... %x; One item of Hash ... One would normally say 'Taking a reference to an array'. ... $$r; Referencing one hash item ... These are both simply lists. ...
    (comp.lang.perl.misc)
  • Re: Making variables private
    ... you've done is made a copy of the pointer/reference. ... Both variables are referencing the same memory/hash. ... What you want to do is copy the hash, not copy the reference to it. ...
    (perl.beginners)
  • Problem with anonymous array in hash
    ... Running this on the command line gives: ... The first number is, as expected, the number of elements in the array ... anonymous array stored in the hash, but obviously I am not referencing ...
    (comp.lang.perl.misc)