Schuk has asked for the wisdom of the Perl Monks concerning the following question:

Ok I am still breeding on my array of hashes and the references...
I decided that the array of hashes is too small for me.. I need to store more values...
In fact I need now an array of hashes of values AND a array of hashes

so it should look like:
array[1]{"Company"} = "CompanyA" #Name of company A array[1]{"Street"} = "StreetA" #Street of company A array[1]{"Contact"}array[1]{"Contact_Tel"} = "4654" #Tel of Contact +1 from Comp A array[1]{"Contact"}array[1]{"Contact_Name"}="Mr B" #Name of Contact + 1 from Comp A array[1]{"Contact"}array[2]{"Contact_Tel"}= "6544" #Tel of Contact +2 from Comp A

i already created my second hash but i dont know how to push it into the hash
it says:
Type of arg 1 to push must be array (not hash element) at H:\legal\xlsparse.pl line 78, near "%contacthash;"
So I need to treat the hash as an array.. or something like that!?...... ARGH CONFUSING
it got to do something with references

I am already able to store it as a reference to the hash but then the idea of a array would be lost...

Anyway, is it possible to create that array? Or should I use a more intelligent way to store my data?

Cheers Schuk

Replies are listed 'Best First'.
Re: Array of hashes of values AND Arrays of Hashes
by broquaint (Abbot) on Oct 11, 2002 at 12:27 UTC
    If you haven't already, check out perlreftut for a superb tutorial on references. In answer to your question you need to dereference what you're pushing onto e.g
    my $hash = { array => [ qw( foo bar ) ] }; push @{ $hash->{array} }, qw( baz quux ); print "\$hash->{array} is: @{ $hash->{array} }\n"; __output__ $hash->{array} is: foo bar baz quux

    HTH

    _________
    broquaint

Re: Array of hashes of values AND Arrays of Hashes
by busunsl (Vicar) on Oct 11, 2002 at 12:33 UTC
    Something like:
    $array[1]->{Contact} = []; %hash = ( Contact_Tel => '1234', Contact_Name => 'Mr. Bean'); push @{$array[1]->{Contact}}, \%hash;
    $array[1]->{Contact} contains the reference to an empty array.
    Since push requires a 'real' array as its first argument, you have to dereference the reference in $array[1]->{Contact}.
    You do this with @{...}.

    See: here and here for more.