: Hi,
: Recently a friend of mine asked me this question. I know the
: difference between the normal arrays and associative arrays. But i
: want to know whether can i use an integer as key in the associative
: arrays?. If so, what is the difference between accessing the first
: element of an array as a[1] and accessing a associative array b
: with key 1 as b{1} ?
Hi Shashi,
Please read a bit of the following lines before your question gets answered!
Under any circumstances you can answer parts of your question by writing
a few lines of code using the variables which you want to explore. Make
sure you start your script with the lines
use warnings;
use strict;
and you'll immediately notice that you should declare your variables and
start them with the indicator for scalars, a dollar sign. While Perl allows
bare words under certain conditions, I rather suggest not to stretch this
feature as it will only cause confusion if you do not know where to look
for the origin of potential problems. So, $scalar is better than scalar, and
beginning with "my $scalar" is even better.
Then, you should always avoid to call your variables $a or $b; Perl has
internal variables of the same name that can be user-manipulated. When-
ever your sort or comparison operation goofs then most certainly due
to indiscriminate use of $a and $b elsewhere in the program.
In addition, did you declare "$[=1;" anywhere in your script prior to
writing "$a[1]"? Most probably, you didn't do so, and hence the first
element of an array is reached by the index [0], not [1]. The index
number in square brackets indicates the _offset_ of the array slice,
not the _ordinal count_ of the array slice. Your $a[1] really indicates
the second element in your array @a. You can set the offset with $[
so that Perl behaves a bit more like awk (see the perlvar manpage);
while this may feel more *natural* to some, it runs across general
expectations and usage.
Now to your question: What is the difference between $value[1] and
$value{1}? In casual use, you won't find any difference, but...
a) Order
Arrays are ordered.
You can always be sure that the element after $value[1] is found
by saying $value[2].
There is no guarantee whatsoever that you can address two consecutive
elements of a hash by saying $hashvalue{1} and then $hashvalue{2}.
Which means: if you want to retrieve the values of a hash in ordered
manner, you have to sort the keys first. Study perlfunc and
perldoc -f keys. You will note that virtually all code iterating over
hash values has a line that starts with:
foreach $element (sort keys %myhash) {
...
}
b) Slices
You can "cut" slices out of an array by saying @dayofweek[3,4,5] which
will return a list with three elements; similar things are possible
with a hash. Again, see man perldata.
c) Special values
You can find the last element of an array by using the $#array notation;
you can't do so with a hash because a hash is not ordered. There is only
a most recently inserted element, but no last element.
Beyond these, there are certainly more intricate and subtle differences.
It is also good to have a look at the posting guidelines which are posted
here regularly. Conforming with this will substantially increase the feed-
back to your postings.
Oliver.