[comp.lang.perl] associative arrays ?

cwilson@NISC.SRI.COM (Chan Wilson) (08/02/90)

I'm fuzzy on the concept of associative arrays, and although I haven't
looked deeply, haven't found a definition in any of the texts I've looked
into. 

What I'm trying to do is simulate a two dimensional array, like so:

@space[$fileserver,$user]=`rsh $fileserver du -s \
	/export/home/$fileserver/$user`


On another topic, is there any way to do something like:

foreach $machine ('fs1','fs2','fs3') { @$machine[$i++]=`rsh $machine users`; }
				       ^^^^^^^^^
					offending statement.

Simple questions?

--Chan
			   ................
    Chan Wilson -- cwilson@nisc.sri.com <!> I don't speak for SRI.
Janitor/Architect of comp.binaries.apple2 archive on wuarchive.wustl.edu
			      "a2fx it!"
			   ................

stef@zweig.exodus (Stephane Payrard) (10/19/90)

Is length()  supported when applied to an associative array?
Why does it return a value equal to : number of keys + number of
values + 1 ?

example:
%  perl -e '%a=(1,2);print length %a;'
3


Why associative arrays are not interpreted in double quotes when
in the same context regular arrays? I feel it makes things inconsistent.

example
% perl -e '%a=(1,2); print "%a\n";@a=(1,2);print "@a";'
%a
1 2


        Thanks
--
Stephane Payrard -- stef@eng.sun.com -- (415) 336 3726
SMI 2550 Garcia Avenue M/S 10-09  Mountain View CA 94043

                     
                     

stef@zweig.sun (Stephane Payrard) (10/28/90)

I did not get any answer te following  questions.
I suppose it did not make it to the main netnews network.
So I repost them.


Is length()  supported when applied to an associative array?
Why does it return a value equal to : number of keys + number of
values + 1 ?

example:
%  perl -e '%a=(1,2);print length %a;'
3


Why associative arrays are not interpreted in double quotes when
in the same context regular arrays? I feel it makes things inconsistent.

example
% perl -e '%a=(1,2); print "%a\n";@a=(1,2);print "@a";'
%a
1 2


        Thanks


                     
                     
--
Stephane Payrard -- stef@eng.sun.com -- (415) 336 3726
SMI 2550 Garcia Avenue M/S 10-09  Mountain View CA 94043

                     
                     

lwall@jpl-devvax.JPL.NASA.GOV (Larry Wall) (10/28/90)

In article <STEF.90Oct27161308@zweig.sun> stef@zweig.sun (Stephane Payrard) writes:
: Is length()  supported when applied to an associative array?

It's not even supported when applied to a normal array.

: Why does it return a value equal to : number of keys + number of
: values + 1 ?

It doesn't, except by coincidence.

: example:
: %  perl -e '%a=(1,2);print length %a;'
: 3

The value of scalar %a is "num_buckets_occupied/num_buckets_allocated", or
"0" for an empty array.  For (1,2) it happens to be "1/4", which has a
length of 3.

I could conceivable keep track of the length of an associative array,
and return that, but I don't.  The main reason is dbm files.  Where would
I store the current length?  What if a C program updates the dbm file?
Or would I have to scan the whole dbm file whenever anyone said "if %a"?

Shudder.

: Why associative arrays are not interpreted in double quotes when
: in the same context regular arrays? I feel it makes things inconsistent.
: 
: example
: % perl -e '%a=(1,2); print "%a\n";@a=(1,2);print "@a";'
: %a
: 1 2

What's the matter with inconsistency?  After all, I'm basically irrational...

There are two reasons for not making it consistent, actually.  One is simply
that it wasn't that way before, and to change it would break things
gratuitously.  (Especially since the % character occurs more frequently
in ordinary text than @ does.  (Not to mention printf formats.  Do you
really want all your printf formats to break when you decide to add a %s
or *s variable to your program?))

The other reason is that people usually don't want that format for printing
out associative arrays anyway.  So why supply it?  If I did add it,
I'd probably make it print out "1=2".

There are many other differences between associative arrays and normal
arrays.  It's only a coincidence that you can pass them both around as
lists.

I'd much rather add a pair() operator.  Or some generalization thereof.

	print join("\n", pair('=', %ENV));

Larry