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

Difference between a hash and an array?

Expand Messages
  • EDJ
    Hi there, This might seem like a stupid question for you diehards out there. But could anybody explain me the difference between a hash and an array? your help
    Message 1 of 3 , Jul 3, 2001
    • 0 Attachment
      Hi there,
      This might seem like a stupid question for you diehards out there.
      But could anybody explain me the difference between a hash and an array?
      your help is highly appreciated.
      greetings,
      EDJ
    • erik.tank@bpxinternet.com
      EDJ ... The only stupid thing you could have done is not ask. Ignorance is stupid, questions aren t. A hash and an array are basically the same thing - a
      Message 2 of 3 , Jul 3, 2001
      • 0 Attachment
        EDJ ... The only stupid thing you could have done is not ask. Ignorance is
        stupid, questions aren't.

        A hash and an array are basically the same thing - a grouping of data that
        is accessed through an index.

        I would try to explain, but probably screw it up somehow so I am going to
        quote Perl in a Nutshell (Chpt 4):
        -> Arrays are ordered lists of scalars that you access with a numeric
        subscript (subscripts start at 0). They are preceded by an "at" sign (@).
        -> Hashes are unordered sets of key/value pairs that you access using the
        keys as subscripts. They are preceded by a percent sign (%).

        Examples:
        @array = (zero,one,two,three);
        print $array[0]; # prints "zero"
        print $array[3]; # prints "three"

        %hash = ( 'First Name' => 'Erik',
        'Last Name' => 'Tank');
        print $hash{'First Name'}; # prints "Erik"
        #NOTE: the hash assignment above could also have been written
        # %hash = ('First Name', 'Erik', 'Last Name', 'Tank');
        # but the way that I have above is clearer.

        Erik Tank
        602-817-4705
        erik.tank@...


        -----Original Message-----
        From: EDJ [mailto:enno@...]
        Sent: Tuesday, July 03, 2001 6:32 AM
        To: perl-beginner@yahoogroups.com
        Subject: [PBML] Difference between a hash and an array?


        Hi there,
        This might seem like a stupid question for you diehards out there.
        But could anybody explain me the difference between a hash and an array?
        your help is highly appreciated.
        greetings,
        EDJ



        Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
      • obrienta@iserv.net
        To clarify the unordered issue, foreach $val ( @array){ print $val, n ; } will print: zero one two three however, foreach $val ( values %hash){ print $val,
        Message 3 of 3 , Jul 5, 2001
        • 0 Attachment
          To clarify the unordered issue,

          foreach $val ( @array){
          print $val, "\n";
          }

          will print:
          zero
          one
          two
          three

          however,

          foreach $val ( values %hash){
          print $val, "\n";
          }

          will print one of the following:

          Eric
          Tank

          OR

          Tank
          Eric

          There is no way to predict which one will print first; hence it is unordered. You can use a sort values %hash to force Eric Tank to print in alphabetic sequence, but what if the first name is changed to Valerie? If sequence is critical, use an array or use a sequential key on the hash and sort by the key.

          Tommy O
          --- In perl-beginner@y..., erik.tank@b... wrote:
          > EDJ ... The only stupid thing you could have done is not ask. Ignorance is
          > stupid, questions aren't.
          >
          > A hash and an array are basically the same thing - a grouping of data that
          > is accessed through an index.
          >
          > I would try to explain, but probably screw it up somehow so I am going to
          > quote Perl in a Nutshell (Chpt 4):
          > -> Arrays are ordered lists of scalars that you access with a numeric
          > subscript (subscripts start at 0). They are preceded by an "at" sign (@).
          > -> Hashes are unordered sets of key/value pairs that you access using the
          > keys as subscripts. They are preceded by a percent sign (%).
          >
          > Examples:
          > @array = (zero,one,two,three);
          > print $array[0]; # prints "zero"
          > print $array[3]; # prints "three"
          >
          > %hash = ( 'First Name' => 'Erik',
          > 'Last Name' => 'Tank');
          > print $hash{'First Name'}; # prints "Erik"
          > #NOTE: the hash assignment above could also have been written
          > # %hash = ('First Name', 'Erik', 'Last Name', 'Tank');
          > # but the way that I have above is clearer.
          >
          > Erik Tank
          > 602-817-4705
          > erik.tank@b...
          >
          >
          > -----Original Message-----
          > From: EDJ [mailto:enno@s...]
          > Sent: Tuesday, July 03, 2001 6:32 AM
          > To: perl-beginner@y...
          > Subject: [PBML] Difference between a hash and an array?
          >
          >
          > Hi there,
          > This might seem like a stupid question for you diehards out there.
          > But could anybody explain me the difference between a hash and an array?
          > your help is highly appreciated.
          > greetings,
          > EDJ
          >
          >
          >
          > Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
        Your message has been successfully submitted and would be delivered to recipients shortly.