5% assure discount from here only

Sunday, January 30, 2011

(18) Difference between Array and Hash?

Solution :

Both datatypes are used in PERL. Both of them are very useful in their respect.

Hash vs Array :-

(i) The basic differnce is the order maintain by both, array always keeps the order of elements inserted but hash will not do the same.

(ii) If data is at large level, than hash is best option as compare to array and at small level, traversing in array is much quicker than hash.

(iii) The symbol difference is there, array always nominated by @ whereas hash is denoting by % symbol.

(iv) The brackets differnce is there, array uses [] parenthesis whereas hash uses {} curly braces.

(v) The elements can be access from array by using index ($array[1]) whereas in hash use keys ($hash{'keys'}) for the same.

(vi) Declaration of both :

my @array = (1,2,3,4,5,6);

foreach my $i(@array){
print $array[$i];
}

my %hash = (1,2,3,4,5,6);

foreach my $keys(keys %hash){
print $keys." => ".$hash{$keys}."\n";
}

6 comments:

  1. You said all that but you forgot to mention the major difference between arrays and hashes.

    An array stores an ordered list of scalar values. Elements of the array are accessed using an integer which is the index of the element in the array.

    A hash stores an unordered list of scalar values. Each value is stored against a string called the key and is accessed using that key.

    ReplyDelete
    Replies
    1. The same thing is written in point V but in technical way.

      Delete
  2. 5th point is all about the thing, which you had asked.

    ReplyDelete
  3. I didn't ask anything.

    Your fifth point is about syntax. My point is about fundamental differences.

    ReplyDelete
  4. though it's a good description... Thanks

    ReplyDelete
  5. Its very nice explanation.... very easy to understand.... Thanks

    ReplyDelete