Date: Wed, 4 Jul 2001 19:29:38 -0700
From: William Pietri <william-news-102374@scissor.com>
Subject: Re: changing hash dynamically
Message-Id: <tk7k8jcvudda5b@corp.supernews.com>

Umair Tariq Bajwa wrote:
> 
> Philip Newton wrote:
> 
>>
>>     my(@info);
>>
>>     my $counter = 0;
>>
>>     foreach my $parameter (@paramlist)
>>     {
>>         if ($parameter =~ /employee_id$counter/)
>>         {
>>             $counter++;
>>         }
>>
>>         $info[$counter]{$parameter} = param($parameter);
>>     }
>
> How can i print the values if i am using autovivification? I have an @info
> and at each index i have reference to hash? I am trying to do it but it
> won't work? Need some help
> 

Howdy, Umair; forgive me for rearranging your message, but it's a usenet 
tradition to put the responses in at the end. That way the messages make 
sense when read through from the beginning.

If you need to print out the contents of @info, you could do it like this:

    foreach my $hash (@info) {
        foreach my $key (keys(%{$hash})) {
            print "$key\t",${$hash}{$key},"\n";
        }
    }

Or if you need to keep track of which parameter goes with which counter and 
wanted to be a little safer, then you could do it like this:

    # loop through @info array
    foreach my $index (0..$#info) {
 
        # make sure we have a hash; print header
        if (ref($info[$index]) eq "HASH") {
            print "info $index contains:\n";
        } else {
            print "info $index isn't a hash.\n";
            next;
        }

        # loop through hash and print
        foreach my $key (keys(%{$info[$index]})) {
            print "\t$key\t", ${$info[$index]}{$key},"\n";
        }
    }


Hope that helps!

William


