Array of hashes?

Array of hashes?

am 06.09.2007 22:01:39 von Neil Cherry

I'm trying to do what I think is simple but I can't get to work.
I have an array:

$foo->['a'] = 1001;
$foo->['b'] = "Some string";

push(@arr, \%foo);

$bar = pop(@arr);

print "B = $bar->{'b'}\n";

Am I missing something obvious (except to me)? I am searching
but not finding much (except an answer on Perl Monks that looks
exactly the same as my code above).

Thanks

--
Linux Home Automation Neil Cherry ncherry@linuxha.com
http://www.linuxha.com/ Main site
http://linuxha.blogspot.com/ My HA Blog
Author of: Linux Smart Homes For Dummies

Re: Array of hashes?

am 06.09.2007 22:08:42 von pacman

In article ,
Neil Cherry wrote:
>I'm trying to do what I think is simple but I can't get to work.
>I have an array:
>
>$foo->['a'] = 1001;
>$foo->['b'] = "Some string";
>
>push(@arr, \%foo);
>

$foo is an arrayref, not a hashref. Even if it was a hashref, it wouldn't be
referring to %foo (unless you'd previously done $foo=\%foo). Both of the
array subscripts are 0 since that's what you get when you use a string that
doesn't look like a number in a numeric context.

You probably meant

$foo->{'a'} = 1001;
$foo->{'b'} = "Some string";

push(@arr, $foo);

>
>$bar = pop(@arr);
>
>print "B = $bar->{'b'}\n";
>

--
Alan Curry
pacman@world.std.com

Re: Array of hashes?

am 06.09.2007 22:17:35 von Mirco Wahab

Neil Cherry wrote:
> I'm trying to do what I think is simple but I can't get to work.
> I have an array:
>
> $foo->['a'] = 1001;
> $foo->['b'] = "Some string";
> push(@arr, \%foo);
> $bar = pop(@arr);
> print "B = $bar->{'b'}\n";

The variable "foo" has to be a *hash*
if you want it that way ...

...
my %foo; # let there be hash
$foo{a} = 1001;
$foo{b} = "Some string";

my @arr;
push(@arr, \%foo);

my $bar = pop(@arr);
print "B = $bar->{b}\n"; # now it works
...

Regards

M.

Re: Array of hashes?

am 06.09.2007 22:33:32 von Lawrence Statton

Neil Cherry writes:
> I'm trying to do what I think is simple but I can't get to work.
> I have an array:
>
> $foo->['a'] = 1001;
> $foo->['b'] = "Some string";

Mmmmmmm ... is $foo an arrayref ( indexes like 0, 1, 2, 3, ... ) or is
$foo a hashref ( keys like 'a', 'b', 'mairzydoats' )?

What you have now is $foo is an arrayref with one value 'Some string'
at index zero.

>
> push(@arr, \%foo);

Now @arr has at its end a reference to some hash you have made no
mention of anywhere in your posting. None of us can come CLOSE to
telling you what values it holds.

>
> $bar = pop(@arr);
>
> print "B = $bar->{'b'}\n";
>

Since we don't know what was in %foo, we can't guess what might be at
key $foo{b}


> Am I missing something obvious (except to me)? I am searching
> but not finding much (except an answer on Perl Monks that looks
> exactly the same as my code above).
>

Well, two things you are missing: $arrayref->['any string value'] is
the same as $arrayref->[0]

(and had you used warnings the "Argument "a" isn't numeric in
array element at /tmp/foo.pl line 9. Argument "b" isn't numeric in
array element at /tmp/foo.pl line 10." would have been a bit of a
dead giveaway)

And perhaps more importantly $foo->{x} has (nearly absolutely)[1] nothing
to do with %foo

my %foo;
$foo{x} = 'Hash';
my $foo;
$foo->{x} = 'Hashref';

print "$foo{x} $foo->{x}\n"

[1] Some know-it-all will point out that they share a slot in the
symbol table, along with , sub foo {} and @foo

--
Lawrence Statton - lawrenabae@abaluon.abaom s/aba/c/g
Computer software consists of only two components: ones and
zeros, in roughly equal proportions. All that is required is to
place them into the correct order.

Re: Array of hashes?

am 06.09.2007 23:20:50 von Neil Cherry

On Thu, 06 Sep 2007 22:17:35 +0200, Mirco Wahab wrote:
> Neil Cherry wrote:
>> I'm trying to do what I think is simple but I can't get to work.
>> I have an array:
>>
>> $foo->['a'] = 1001;
>> $foo->['b'] = "Some string";
>> push(@arr, \%foo);
>> $bar = pop(@arr);
>> print "B = $bar->{'b'}\n";
>
> The variable "foo" has to be a *hash*
> if you want it that way ...
>
> ...
> my %foo; # let there be hash
> $foo{a} = 1001;
> $foo{b} = "Some string";
>
> my @arr;
> push(@arr, \%foo);
>
> my $bar = pop(@arr);
> print "B = $bar->{b}\n"; # now it works
> ...

DOH! You are quite correct, I shouldn't have been using [] when I
wanted {} instead. I bet I did the same thing in my code. Thanks!

--
Linux Home Automation Neil Cherry ncherry@linuxha.com
http://www.linuxha.com/ Main site
http://linuxha.blogspot.com/ My HA Blog
Author of: Linux Smart Homes For Dummies

Re: Array of hashes?

am 06.09.2007 23:21:45 von Neil Cherry

On Thu, 6 Sep 2007 20:08:42 +0000 (UTC), Alan Curry wrote:
> In article ,
> Neil Cherry wrote:
>>I'm trying to do what I think is simple but I can't get to work.
>>I have an array:
>>
>>$foo->['a'] = 1001;
>>$foo->['b'] = "Some string";
>>
>>push(@arr, \%foo);
>>
>
> $foo is an arrayref, not a hashref. Even if it was a hashref, it wouldn't be
> referring to %foo (unless you'd previously done $foo=\%foo). Both of the
> array subscripts are 0 since that's what you get when you use a string that
> doesn't look like a number in a numeric context.
>
> You probably meant
>
> $foo->{'a'} = 1001;
> $foo->{'b'} = "Some string";
>
> push(@arr, $foo);

Yep, messed that up real good didn't I. Thanks.

--
Linux Home Automation Neil Cherry ncherry@linuxha.com
http://www.linuxha.com/ Main site
http://linuxha.blogspot.com/ My HA Blog
Author of: Linux Smart Homes For Dummies

Re: Array of hashes?

am 07.09.2007 01:29:01 von Ben Morrow

Quoth Lawrence Statton :



> And perhaps more importantly $foo->{x} has (nearly absolutely)[1] nothing
> to do with %foo
>
> my %foo;
> $foo{x} = 'Hash';
> my $foo;
> $foo->{x} = 'Hashref';
>
> [1] Some know-it-all will point out that they share a slot in the
> symbol table, along with , sub foo {} and @foo

Nope, instead I'll point out that neither $foo nor %foo reside in the
symbol table at all, they both reside in the lexical pad, and that they
have really really absolutely nothing to do with each other. :P

Ben

Re: Array of hashes?

am 07.09.2007 01:35:29 von Tad McClellan

Neil Cherry wrote:
> On Thu, 6 Sep 2007 20:08:42 +0000 (UTC), Alan Curry wrote:
>> In article ,
>> Neil Cherry wrote:
>>>I'm trying to do what I think is simple but I can't get to work.
>>>I have an array:
>>>
>>>$foo->['a'] = 1001;
>>>$foo->['b'] = "Some string";
>>>
>>>push(@arr, \%foo);
>>>
>>
>> $foo is an arrayref, not a hashref. Even if it was a hashref, it wouldn't be
>> referring to %foo (unless you'd previously done $foo=\%foo). Both of the
>> array subscripts are 0 since that's what you get when you use a string that
>> doesn't look like a number in a numeric context.
>>
>> You probably meant
>>
>> $foo->{'a'} = 1001;
>> $foo->{'b'} = "Some string";
>>
>> push(@arr, $foo);
>
> Yep, messed that up real good didn't I. Thanks.


You should always enable warnings when developing Perl code.

You should *especially* enable warnings when Perl is behaving
in a way that you do not understand.

The machine will help you find your bugs, but only if you ask it to.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"