On Feb 13, Barry Kingsbury said:

>I have created the following data structure:
>
>%mailings = ( tv => { mail_demo_key   => "Demo License Key for TotalView",
>                      mail_thank_you  => "Thank you for downloading
>TotalView",
>                      pdf             => LOCATION_OF_PDF,
>                      location        => LOCATION_OF_DEMO_REPLY,
>                      text            => LOCATION_OF_INSTRUCT,
>                      text_brochure   => LOCATION_OF_BROCHURE },
>              cr => { mail_demo_key  => "Demo License Key for JNI Bridge
>and TotalView",
>                      mail_thank_you => "Thank you for downloading the
>CodeRoad JNI Bridge",
>                      pdf            => LOCATION_OF_CR_PDF,
>                      location       => LOCATION_OF_CR_DEMO_REPLY,
>                      text           => LOCATION_OF_CR_INSTRUCT,
>                      text_brochure  => LOCATION_OF_CR_BROCHURE }
>            );

It's scary to build a data structure you don't know how to access.

  %hash = (
    key1 => {
      subkey1 => 'value 1',
      subkey2 => 'value 2',
    },
    key2 => {
      subkey1 => 'value A',
      subkey2 => 'value B',
    },
  );

That's the same basic idea as your hash above.

>      print "$key => " . %{$mailings{$form_name}}->{$key} . "\n";

That's a crufty, not-supposed-to-work syntax.

>      print "$key => " . ${$mailings{$form_name}}{$key} . "\n";

That's more work than necessary.

>I have no idea what this syntax is saying or doing and why the two
>different forms of the hash reference are working.
>
>Can some guru explain?

<guru>

The first one works "accidentally".  %H->{key} works the same way
$hash{key} does, but it's an anomoly, since the LHS (left-hand side) of ->
is SUPPOSED to be a reference.  Replacing 'H' with $hash{key1} gives us
the syntax you had, %{ $hash{key1} }->{subkey1}.

The second works logically.  Start with $H{key}, and replace 'H' with
$hash{key1} and you get ${ $hash{key1} }{subkey1}.

But here's the kicker.  When you have a value from a hash or an array, and
it's a reference, and you want to get at some value further embedded in
it, you don't need all those {}'s and ->'s to get the job done.

$hash{key1}{subkey1} works the same as ${ $hash{key1} }{subkey1}, but
looks a whole lot nicer.

</guru>

For more fun-time reference reading, check out

  perldoc perlreftut
  perldoc perlref
  perldoc perldsc
  perldoc perllol

(The last one is NOT about humor etiquette in the Perl community.)

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to