Indexing hashtables

0 views
Skip to first unread message

Klaas-Jan Stol

unread,
Jun 22, 2005, 2:25:51 PM6/22/05
to Internals List
hi,

I have some trouble indexing hashtables.

I have the following code snippet:

.sub main
.local pmc x
x = new .Hash

.local pmc y
y = new .Hash
x["y"] = y

$P10 = new .Integer
$P10 = 1

x["y;a"] = $P10 # (1) this does not work with the code below
y["a"] = $P10 # (2) this does work with the code below

# accessing x.y.a in 2 steps
$P99 = x["y"]
$P98 = $P99["a"]
printerr $P98

end
.end


x.y.a = 1;
print(x.y.a);

x and y are both hashtables.
x has a field "y", storing y
y has a field "a", storing an integer

I'd like to get the field "a" step by step, so first get a reference to
x.y, then get a reference to y.a, instead of indexing at once with "y;a"
as done at (2). Doing it step by step makes generating code a bit
easier, in my opinion.

I'm wondering what I'm doing wrong, because it looks like it should be
working (right?)

regards,
klaas-jan

Joshua Juran

unread,
Jun 22, 2005, 8:25:11 PM6/22/05
to perl6-i...@perl.org
On Jun 22, 2005, at 2:25 PM, Klaas-Jan Stol wrote:

> I have some trouble indexing hashtables.
>
> I have the following code snippet:
>

> x["y;a"] = $P10 # (1) this does not work with the code below
> y["a"] = $P10 # (2) this does work with the code below
>
>

> x.y.a = 1;
> print(x.y.a);
>
> x and y are both hashtables.
> x has a field "y", storing y
> y has a field "a", storing an integer
>
> I'd like to get the field "a" step by step, so first get a reference
> to x.y, then get a reference to y.a, instead of indexing at once with
> "y;a" as done at (2). Doing it step by step makes generating code a
> bit easier, in my opinion.
>
> I'm wondering what I'm doing wrong, because it looks like it should be
> working (right?)

I'm just guessing here, but doesn't #1 above add 1 to x with key "y;a"?
For example, if your intention in Perl 5 was '$x->{y}{a} = 1', doesn't
#1 mean '$x->{"y;a"} = 1'?

Josh

Leopold Toetsch

unread,
Jun 23, 2005, 3:00:35 AM6/23/05
to Klaas-Jan Stol, Internals List
Klaas-Jan Stol wrote:

> x["y;a"] = $P10 # (1) this does not work with the code below

You want:

x["y"; "a"] = $P10

leo

Reply all
Reply to author
Forward
0 new messages