in reply to Re^2: Spliting a delimited string into variables
in thread Spliting a delimited string into variables

Initializing a hash can be done with an array, each pair of values in the array form key and value of the hash. Often '=>' is used as an alias to ',' to make this obvious:

my %hash= (0, 'ticket', 1 => 'dateadded');

{$names{$_}} on the other hand is not a really useful expression in perl, %{$names{$_}} refers to a hash whose reference is stored as value in another hash (i.e %names). A multidimensional hash or HashOfHashes in perlspeak

If what you wanted was a Hash of Hashes, the syntax looks like this:

$names{$_}{key}= 'value'; #or to initialize with an array: %{$names{$_}} = ('key1' => 'value1','key2' => 'value2');

if you want further information about HoH (Hash of Hashes), you might want to read perldsc and perllol

Replies are listed 'Best First'.
Re^4: Spliting a delimited string into variables
by pissflaps (Initiate) on Apr 08, 2011 at 21:23 UTC

    I've never used hashes before so this will be some good fresh reading material. It looks relative to a really complex array. In your experience with hashes, once you have all the structure laid out, is it simple enough to reference slices in the hash like you would in an array? That is to say $names[0][1] would pull up the value in the second column of the first row of the matrix? Thank you again for your great references and starting points. I am excited to learn!

      Hashes are one of the most used features in perl and once you find out how useful they are you will never want to program in a language without hashes again.

      And yes, access is as simple as with arrays, $names{phil}{street} will get you phils street name, if you put it in there. Note that arrays have still their use, for example to store a matrix. Or for any data you need a sorted or defined sequence. But in my experience for data structures in 4 out of 5 cases a hash is the answer.