Forum Moderators: coopster & phranque

Message Too Old, No Replies

Arrays of Hashes - STUMPED Please Help

Making array of hashes and returning from subroutine ...

         

physics

9:16 pm on Jun 8, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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!!!

sugarkane

6:46 pm on Jun 9, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First thought: you've spelt seperator as 'separator' in your pattern match - if this isn't just a typo in your post, that might be your problem.

(Actually, I lied - that wasn't my first thought, it was about my 28th, I was stumped too!)

physics

2:37 pm on Jun 11, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks sugarkane, but that was only a typo in the post, not in the program. Still haven't figured this out. One thing I did was test that the array of hashes was being built correctly, and it was ... so it must be how I am returning it or something. Should I return a reference to the array instead of the array?

Cheers!

Brett_Tabke

5:03 pm on Jun 12, 2001 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Sorry you stumped us on this one physics. LOL's (lists of lists) are tricky things that most of us never use.

physics

5:15 pm on Jun 12, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, thanks for all your support! It turns out that the above code is sound (I think) and the error was due to some other dumb thing I was doing. I should probably avoid lists of hashes, but I can't help it. They're so 'elegant'

Cheers

sugarkane

5:33 pm on Jun 12, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>the above code is sound (I think)

I think so ;)

I've been playing with it and simply couldn't reproduce your problem.

After calling your subroutine, doing:

print "$other_array[1]{product}\n";
print "$other_array[2]{category}\n";
print "$other_array[3]{product}\n";
print "$other_array[4]{category}\n";

resulted in:

example product
example category 2
example product 3
example category 4

..which I assume is what you wanted?

Anyway - thanks for posting this, it got me to look into an area I've not really touched before :)