array reference expected

B

bnapus

Perlers -

I want to assign a value to an annonymous hash:
$hash->[$i][$j]{Score} = 1;

Why won't it accept a scalar? I get an error about perl expecting an
address to an array. What I envisioned was a hash within a 2D array.

I ended up using this:
$hash->[$i]{$j}{Score} = 1;

and it does what I want it to - just curious.

Josh
 
B

Brian Harnish

Perlers -

I want to assign a value to an annonymous hash:
$hash->[$i][$j]{Score} = 1;

Why won't it accept a scalar? I get an error about perl expecting an
address to an array. What I envisioned was a hash within a 2D array.

I ended up using this:
$hash->[$i]{$j}{Score} = 1;

and it does what I want it to - just curious.

Josh

Without seeing the rest of your code, I don't have an answer for you. But
yes, it is possible to do what you want to do:
perl -MData::Dumper -we '$hash->[0][0]{Score}=1;print Dumper $hash'

I believe you are supposed to breakdown your problem to a simple script
that shows the problem, then post that script. Often durring this process,
you find the error yourself.
- Brian
 
B

Ben Morrow

I want to assign a value to an annonymous hash:
$hash->[$i][$j]{Score} = 1;

This implies $hash is a
reference to an array containing
references to arrays containing
references to hashes,
i.e., that the structure you want is

$hash = [
($i - 1 elements),
[
($j - 1 elements),
{
Score => 1
}
]
];
Why won't it accept a scalar? I get an error about perl expecting an
address to an array. What I envisioned was a hash within a 2D array.

I ended up using this:
$hash->[$i]{$j}{Score} = 1;

....which implies that the structure you actually have is

$hash = [
($i - 1 elements),
{
$j => {
Score => 1
}
}
];

.. The error about 'reference to an array' is referring to the contents
of $hash->[$i]: by attempting to dereference it with [$j] you are
assuming it contains an arrayref, when it seems as though it in fact
contains a hashref.
and it does what I want it to - just curious.

I don't think it does. For a start, the elements of $hash->[$i] are
ordered in the structure you (thought you) wanted, whereas here they
are not.

I think you have misunderstood either your data structure or how
references work in Perl. Add 'use Data::Dumper;' to the top of your
script and 'print Dumper $hash;' just before this line. Then carefully
re-read perllol and perlreftut again, and compare with what you see.

If you still don't understand, then explain carefully what you are
trying to achieve with this data structure and show the code you have
at present, and someone will be better placed to tell you which one
you meant and what is wrong with your code.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top