Loading ...
Sorry, an error occurred while loading the content.
 

Hash of arrays

Expand Messages
  • kenshail2003
    Assuming that the only prior declaration is my %MyHashOfArrays; how does perl (5.8) interpret this?: $MyHashOfArrays{$key}[$index]=$someValue; Is it equivalent
    Message 1 of 4 , Aug 5, 2006
      Assuming that the only prior declaration is
      my %MyHashOfArrays;
      how does perl (5.8) interpret this?:
      $MyHashOfArrays{$key}[$index]=$someValue;

      Is it equivalent to
      $MyHashOfArrays{$key}->[$index]=$someValue;
      with autovivication occurring?

      The former seems to work but with unpredictable initialisation.
      For example
      MyHashOfArrays=();
      does not appear to empty the array elements.
      By experiment
      MyHashOfArrays=([]);
      does appear to.
    • kenshail2003
      ... Correction to my posting: should have been %MyHashOfArrays=(); and %MyHashOfArrays=([]);
      Message 2 of 4 , Aug 6, 2006
        --- In perl-beginner@yahoogroups.com, "kenshail2003" <ken@...> wrote:
        >
        > Assuming that the only prior declaration is
        > my %MyHashOfArrays;
        > how does perl (5.8) interpret this?:
        > $MyHashOfArrays{$key}[$index]=$someValue;
        >
        > Is it equivalent to
        > $MyHashOfArrays{$key}->[$index]=$someValue;
        > with autovivication occurring?
        >
        > The former seems to work but with unpredictable initialisation.
        > For example
        > MyHashOfArrays=();
        > does not appear to empty the array elements.
        > By experiment
        > MyHashOfArrays=([]);
        > does appear to.
        >
        Correction to my posting:
        should have been
        %MyHashOfArrays=();
        and
        %MyHashOfArrays=([]);
      • merlyn@stonehenge.com
        ... kenshail2003 Assuming that the only prior declaration is kenshail2003 my %MyHashOfArrays; kenshail2003 how does perl (5.8) interpret this?:
        Message 3 of 4 , Aug 6, 2006
          >>>>> "kenshail2003" == kenshail2003 <ken@...> writes:

          kenshail2003> Assuming that the only prior declaration is
          kenshail2003> my %MyHashOfArrays;
          kenshail2003> how does perl (5.8) interpret this?:
          kenshail2003> $MyHashOfArrays{$key}[$index]=$someValue;

          kenshail2003> Is it equivalent to
          kenshail2003> $MyHashOfArrays{$key}->[$index]=$someValue;
          kenshail2003> with autovivication occurring?

          THe two are identical.

          kenshail2003> The former seems to work but with unpredictable initialisation.
          kenshail2003> For example
          kenshail2003> MyHashOfArrays=();
          kenshail2003> does not appear to empty the array elements.

          Presuming you mean %MyHashOfArrays (as you corrected), that should empty
          the entire hash.

          kenshail2003> By experiment
          kenshail2003> MyHashOfArrays=([]);
          kenshail2003> does appear to.

          Again with your correction, that's actually an error. %x = (list)
          wants an even number of elements in list, because they become key/value
          pairs. With warnings enabled, you'll get a message saying so:

          $ perl -w -e '%x = ([])'
          Reference found where even-sized list expected at -e line 1.

          Yup. An error.

          --
          Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
          <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
          Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
          See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
        • kenshail2003
          ... initialisation. ... empty ... key/value ... I thought that I had warnings enabled by #!/usr/bin/perl -w (I work on both windoze and Linux) But activestate
          Message 4 of 4 , Aug 7, 2006
            --- In perl-beginner@yahoogroups.com, merlyn@... wrote:
            >
            > >>>>> "kenshail2003" == kenshail2003 <ken@...> writes:
            >
            > kenshail2003> Assuming that the only prior declaration is
            > kenshail2003> my %MyHashOfArrays;
            > kenshail2003> how does perl (5.8) interpret this?:
            > kenshail2003> $MyHashOfArrays{$key}[$index]=$someValue;
            >
            > kenshail2003> Is it equivalent to
            > kenshail2003> $MyHashOfArrays{$key}->[$index]=$someValue;
            > kenshail2003> with autovivication occurring?
            >
            > THe two are identical.
            >
            > kenshail2003> The former seems to work but with unpredictable
            initialisation.
            > kenshail2003> For example
            > kenshail2003> MyHashOfArrays=();
            > kenshail2003> does not appear to empty the array elements.
            >
            > Presuming you mean %MyHashOfArrays (as you corrected), that should
            empty
            > the entire hash.
            >
            > kenshail2003> By experiment
            > kenshail2003> MyHashOfArrays=([]);
            > kenshail2003> does appear to.
            >
            > Again with your correction, that's actually an error. %x = (list)
            > wants an even number of elements in list, because they become
            key/value
            > pairs. With warnings enabled, you'll get a message saying so:
            >
            > $ perl -w -e '%x = ([])'
            > Reference found where even-sized list expected at -e line 1.
            >
            > Yup. An error.
            >
            > --
            I thought that I had warnings enabled by
            #!/usr/bin/perl -w
            (I work on both windoze and Linux)
            But activestate 5.8.4 seems to ignore that -w
            I have had to add
            use warnings;
          Your message has been successfully submitted and would be delivered to recipients shortly.