Short answer: Yes.

Medium answer: Sort of, but they need to references to arrays not arrays

Long answer:

A hash (common term for associative array) contains a scalar key and a
scalar value.  A scalar value can be (not inclusive) a number, a string,
or a reference.  Since we can generate a reference to an array and a
reference can be stored in a scalar and a scalar can be the value of a
hash then yes, you can store arrays as the values of a hash:

<code>
#!/usr/bin/perl -w

use strict;

my %hash = ( 
        'array1' = [0, 1, 2, "three", four], #create a reference to an
                                             #anonymous array of five
                                             #values
        'array2' = ['hello', 'world']        #ditto, but two values
);

push @{$hash{'array1'}}, "five"; #push a value onto the first array
</code>

On 26 Jun 2001 13:29:39 +0100, Paul Murphy wrote:
> 
> 
> Hi all.
> 
> Is there any way in Perl to set up an associative array of normal arrays?
> 
> So the key would be an arbitrary string, and the value for instance would be an 
>array of numbers.
> 
> I'll explain what I want to do, as their may well be better ways:
> 
> Suppose I have a file like this:
> 
> Hello; 1
> Goodbye; 1
> Hello; 2
> Hello; 2
> Hello; 3
> Goodbye; 2
> Hello; 1
> Goodbye; 1
> etc...
> 
> Basically, a string, with an associated number.  I want to count the amount of 
>Hello's with a 1 next to them, and the amount of Hello's with a 2 next to them etc.
> 
> Only, I don't know what the string in the file is going to be up front.
> 
> I can count the instances of each string easily enough, ie, how many hello's there 
>are, but I can't see how to count Hello 2's.
> 
> I can explain this more clearly if required, as I read it and it only barely makes 
>sense to me.
> 
> Thanks for any tips,
> 
> Paul.
> 
> 
> 
> 
>---------------------------------------------------------------------------------------------------------------------------
> CRESTCo Ltd.             The views expressed above are not necessarily those
> 33 Cannon Street.        held by CRESTCo Limited.
> London  EC4M 5SB (UK)      
> +44 (020) 7849 0000     http://www.crestco.co.uk 
> 
>---------------------------------------------------------------------------------------------------------------------------
> 
--
Today is Boomtime, the 31st day of Confusion in the YOLD 3167
Fnord.


Reply via email to