-----Original Message-----
From: Bryan Gmyrek 
Sent: Friday, June 08, 2001 5:15 PM
To: '[EMAIL PROTECTED]'
Subject: Arrays of Hashes


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(/seperator/){
                        $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 artist

seperator

category: example category 2

product: example artist 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?  Is it how I am returning the
array?  Should I return a reference to the array or something like that?


Thanks!!!

Bryan

Reply via email to