I've written a program where I need to use an array of hashes.  The
basic code I am having problems with is:

sub read_from_file{
#input: file name that holds lines of info in the form  key: value
#action: add these to an array of hashes
#return: array of hashes
        my $file = $_[0];
        my @inputs;
        my $i=0;
        open(IN,"$file");
        while(<IN>){
                chomp;
                if(/separator/){
                        $i++;
                }
                elsif(/category\:\s/){
                        $inputs[$i]{category}=$';
                }
                elsif(/artist\:\s/){
                        $inputs[$i]{product}=$';
                }
        }
        close(IN);
        return @inputs;
}

The input file is of the form:

seperator

category: example category

product: example product

seperator

category: example category 2

product: example product 2

and so on

So, in the end I think you should end up with an array of hashes in the
form:

@inputs = (
    {
       category     => "example category",
       product   => "example product",
    },
    {
       category    => "example category 2",
       product    => "example product 2",
    },
);


My question is:

Why does it only 'sort of' work?  I call the routine later with:

my @other_array = &read_from_file("my_file");

and then I try to iterate over the new array of hashes and perform some
functions, but only $other_array[1] holds any information
($other_array[0] should not, but $other_array[2] should)  

I'm totally stumped here, any suggestions?

Thanks!!!

Bryan

Reply via email to