Velocity Reviews > Perl > Difference between accessing arrays and associative arrays using a int index

# Difference between accessing arrays and associative arrays using a int index

shashi
Guest
Posts: n/a

 04-12-2006
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} ?

Regards,
Shashi.

John W. Krahn
Guest
Posts: n/a

 04-12-2006
shashi wrote:
>
> 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?.

Yes.

> 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} ?

You probably meant \$a[1] and \$b{1} and the first element of an array is in
\$a[0] and a hash does not have a "first" element. Aside from that there
should be little difference accessing a single element from either.

John
--
use Perl;
program
fulfillment

shashi
Guest
Posts: n/a

 04-12-2006
Sorry John,

i meant is \$a[1] and \$b{2}. When i say \$b{1} does it go directly
to the concerned element to pick up the value as in \$a[1] or not or we
need to calculate the hash for finding the concerned integer key in
\$b{1}.

If u do not understand the question, please tell me how program
access the normal array and associative array

Regards,
Shashi.

Guest
Posts: n/a

 04-12-2006
shashi <(E-Mail Removed)> wrote:
: 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,

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-

Oliver.

--
Dr. Oliver Corff e-mail: http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de

shashi
Guest
Posts: n/a

 04-12-2006
Thanks Oliver for the reply. I am a bit hurry with my work. So i
couldn't follows the above said guidelines. I will make sure that they
will be followed in my next posts.

Bart Lateur
Guest
Posts: n/a

 04-12-2006
shashi wrote:

>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?.

Yes, of course. Any string can be used as a key of an associative array
(AKA hash), and an integer is no exception. It'll be converted to a
string, first.

> 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} ?

Two differences:

1) The hash will be a "sparse array": only the items that get addressed,
will exist. If you create a hash item \$a{1234567}, then there'll be only
one item added. If you create an array item \$a[1234567], then over 1
million items will be created.

2) You must take care that you only use the characters that you want. If
necessary, add 0 to the value before using it as a key. \$a[123] and
\$a[" 123"] have the same value as an index, but \$a{123} and \$a{" 123"}
do not.

Oh, and those hash keys are in as-good-as-random order, as always.

--
Bart.

Jürgen Exner
Guest
Posts: n/a

 04-12-2006
shashi wrote:
> 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?.

Absolutely. Why not?
Conceptionally arrays are a mapping from natural numbers to scalars while
hashes are a generalization that maps arbitrary strings to scalars.

> 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} ?

In practical terms almost none:
- the key will be the string value of the integer, not the numerical value.
That shouldn't matter.
- the array will contain 2 elements while the hash contains only 1. This
becomes significant if you are considering \$arr[500000] versus
\$hash{'500000'}
- and of course hashes are not ordered

jue

A. Sinan Unur
Guest
Posts: n/a

 04-12-2006
"shashi" <(E-Mail Removed)> wrote in
news:(E-Mail Removed) ups.com:

> Thanks Oliver for the reply. I am a bit hurry with my work. So i
> couldn't follows the above said guidelines. I will make sure that they
> will be followed in my next posts.

This is your "next post" and you are still not following them.

Sinan

--
A. Sinan Unur <(E-Mail Removed)>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:

Dr.Ruud
Guest
Posts: n/a

 04-12-2006
shashi schreef:
> 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} ?

As a side note, you could maintain an array parallel to a hash, or v.v.

#!/usr/bin/perl
use strict;
use warnings;

local (\$", \$\) = ("\n", "\n");

my @a = (0, 1, 3, 4, 7, 8, 999 );

my %b =
( 'very first' => \\$a[0]
, 'first' => \\$a[1]
, 'second' => \\$a[2]
, 'third' => \\$a[3]
, 'last' => \\$a[\$#a]
);

push @a, '---';
print "@a";

(\${\$b{'third'}} *= 10) += 9;
print "@a";

my @b;
for (sort keys %b) { push @b, "\$_:\t\${\$b{\$_}}" }
print "@b";
__END__

Is there a nicer way to write those \${\$b{<key>}}-s?

--
Affijn, Ruud

"Gewoon is een tijger."

shashi
Guest
Posts: n/a

 04-12-2006
Hi sorry guys,

i just got sometime to write the question clearly. This is an
of a[0]. where a is a normal array and b is an associative array with
the key as an integer. I mean from the performance point of view, why
would you prefer using an associative array than a normal one, even
when the key of the array is an integer.

Regards,
Shashi.