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

Re: [PBML] Newbie hash question

Expand Messages
  • merlyn@stonehenge.com
    ... j12lee2000 Hello all j12lee2000 my @names=( John , Peter , Mary ); j12lee2000 my @ages =(12, 14, 16); j12lee2000 keys is names j12lee2000 How to
    Message 1 of 7 , Jun 3, 2004
    • 0 Attachment
      >>>>> "j12lee2000" == j12lee2000 <j12lee2000@...> writes:

      j12lee2000> Hello all
      j12lee2000> my @names=("John", "Peter", "Mary");
      j12lee2000> my @ages =(12, 14, 16);

      j12lee2000> keys is names

      j12lee2000> How to create a hash?

      The easiest way is a hash slice:

      my %new_hash;

      @new_hash{@names} = @ages;

      And no, you can't combine that to one statement. :)

      --
      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!
    • eman
      On 03 Jun 2004 17:04:04 -0700, merlyn@stonehenge.com ... why is that: @new_hash{@names} = @ages; instead of: %new_hash{@names} = @ages; ?
      Message 2 of 7 , Jun 3, 2004
      • 0 Attachment
        On 03 Jun 2004 17:04:04 -0700, merlyn@...
        <merlyn@...> wrote:
        >
        > >>>>> "j12lee2000" == j12lee2000 <j12lee2000@...> writes:
        >
        > j12lee2000> Hello all
        > j12lee2000> my @names=("John", "Peter", "Mary");
        > j12lee2000> my @ages =(12, 14, 16);
        >
        > j12lee2000> keys is names
        >
        > j12lee2000> How to create a hash?
        >
        > The easiest way is a hash slice:
        >
        > my %new_hash;
        >
        > @new_hash{@names} = @ages;
        >
        > And no, you can't combine that to one statement. :)

        why is that:
        @new_hash{@names} = @ages;
        instead of:
        %new_hash{@names} = @ages;
        ?
      • merlyn@stonehenge.com
        ... eman why is that: eman @new_hash{@names} = @ages; eman instead of: eman %new_hash{@names} = @ages; eman ? Because the first one is correct, and the
        Message 3 of 7 , Jun 4, 2004
        • 0 Attachment
          >>>>> "eman" == eman <eman@...> writes:

          eman> why is that:
          eman> @new_hash{@names} = @ages;
          eman> instead of:
          eman> %new_hash{@names} = @ages;
          eman> ?

          Because the first one is correct, and the second one isn't. :)

          --
          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!
        • Charles K. Clarkson
          ... Read the Slices section of perldata . HTH, Charles K. Clarkson -- Mobile Homes Specialist 254 968-8328
          Message 4 of 7 , Jun 4, 2004
          • 0 Attachment
            eman <mailto:eman@...> wrote:

            : On 03 Jun 2004 17:04:04 -0700, merlyn@...
            : <merlyn@...> wrote:
            ::
            :: my %new_hash;
            ::
            :: @new_hash{@names} = @ages;
            ::
            :: And no, you can't combine that to one statement. :)
            :
            : why is that:
            : @new_hash{@names} = @ages;
            : instead of:
            : %new_hash{@names} = @ages;
            : ?

            Read the "Slices" section of "perldata".

            HTH,

            Charles K. Clarkson
            --
            Mobile Homes Specialist
            254 968-8328
          • eman
            On Fri, 4 Jun 2004 09:32:28 -0500, Charles K. Clarkson ... Oh, now i see: @colors{ red , blue , green } = (0xff0000, 0x0000ff, 0x00ff00); Thanks! I just
            Message 5 of 7 , Jun 4, 2004
            • 0 Attachment
              On Fri, 4 Jun 2004 09:32:28 -0500, Charles K. Clarkson
              <cclarkson@...> wrote:
              >
              > eman <mailto:eman@...> wrote:
              >
              > : On 03 Jun 2004 17:04:04 -0700, merlyn@...
              > : <merlyn@...> wrote:
              > ::
              > :: my %new_hash;
              > ::
              > :: @new_hash{@names} = @ages;
              > ::
              > :: And no, you can't combine that to one statement. :)
              > :
              > : why is that:
              > : @new_hash{@names} = @ages;
              > : instead of:
              > : %new_hash{@names} = @ages;
              > : ?
              >
              > Read the "Slices" section of "perldata".

              Oh, now i see:
              @colors{'red','blue','green'} = (0xff0000, 0x0000ff, 0x00ff00);
              Thanks!

              I just realized how many lines i've wasted. I really should be reading
              some books instead of relying on online tutorials.
              I'm glad i joined this group, tho.
            • Jenda Krynicky
              From: merlyn@stonehenge.com ... Just for completeness sake here are some more options: my %new_hash; $new_hash{$names[$_]} = $ages[$_] for(0 .. $#names); my
              Message 6 of 7 , Jun 7, 2004
              • 0 Attachment
                From: merlyn@...
                > >>>>> "j12lee2000" == j12lee2000 <j12lee2000@...> writes:
                >
                > j12lee2000> Hello all
                > j12lee2000> my @names=("John", "Peter", "Mary");
                > j12lee2000> my @ages =(12, 14, 16);
                >
                > j12lee2000> keys is names
                >
                > j12lee2000> How to create a hash?
                >
                > The easiest way is a hash slice:
                >
                > my %new_hash;
                >
                > @new_hash{@names} = @ages;

                Just for completeness sake here are some more options:

                my %new_hash;
                $new_hash{$names[$_]} = $ages[$_] for(0 .. $#names);


                my %new_hash = map {$_ => shift(@ages)} @names;
                # this one destroys @ages!!!


                The hash slice is quickest.

                Jenda
                ===== Jenda@... === http://Jenda.Krynicky.cz =====
                When it comes to wine, women and song, wizards are allowed
                to get drunk and croon as much as they like.
                -- Terry Pratchett in Sourcery
              Your message has been successfully submitted and would be delivered to recipients shortly.