Datastructure

B

Bioperler

HI!

Actually I'm about to code a tool, that's able to track users of a
website (using cookies). However, I've got a problem with the sorting of
the data. I need a data structure like this:

%hash(array(array)) "hash of an array of an array"

So for testing purposes I generated a small dataset, which looks like this:


%HoAoA = (
hash1 => [
( [ "first", "second" ],
[ "third" , "fourth" ],
[ "fifth" , "sixth" ]
)
],
hash2 => [
( [ "9th", "10th" ],
[ "11th" , "12th" ]
)
]

);

So while it was no problem to acces all data fields using nested loops,
I was not able to append data to these arrays listed above. Array's push
doesn't seem to work for this.

Example of what I want to do:
push($HoAoA{hash1}[2], "seventh");


Any suggestions?
 
P

Peter Makholm

Bioperler said:
%HoAoA = (
hash1 => [
( [ "first", "second" ],
[ "third" , "fourth" ],
[ "fifth" , "sixth" ]
)
],

You don't need the inner ()'s
hash2 => [
( [ "9th", "10th" ],
[ "11th" , "12th" ]
)
]

);
Example of what I want to do:
push($HoAoA{hash1}[2], "seventh");

The first argument to push should be an actual array and not just an
reference. So you have to dereference it:

push @{ $HoAoA{hash1}[2] }, "seventh";

//Makholm
 
J

John W. Krahn

Bioperler said:
Actually I'm about to code a tool, that's able to track users of a
website (using cookies). However, I've got a problem with the sorting of
the data. I need a data structure like this:

%hash(array(array)) "hash of an array of an array"

So for testing purposes I generated a small dataset, which looks like this:

%HoAoA = (
hash1 => [
( [ "first", "second" ],
[ "third" , "fourth" ],
[ "fifth" , "sixth" ]
)
],
hash2 => [
( [ "9th", "10th" ],
[ "11th" , "12th" ]
)
]

);

So while it was no problem to acces all data fields using nested loops,
I was not able to append data to these arrays listed above. Array's push
doesn't seem to work for this.

Example of what I want to do:
push($HoAoA{hash1}[2], "seventh");

Any suggestions?

You have to dereference the array (push only modifies arrays):

push @{ $HoAoA{ hash1 }[ 2 ] }, 'seventh';



John
 
B

Bioperler

Peter said:
Bioperler said:
%HoAoA = (
hash1 => [
( [ "first", "second" ],
[ "third" , "fourth" ],
[ "fifth" , "sixth" ]
)
],

You don't need the inner ()'s
Yes, you're right. But actually the structure is a bit more complex - I
just shorted it a little bit to ease it.
hash2 => [
( [ "9th", "10th" ],
[ "11th" , "12th" ]
)
]

);
Example of what I want to do:
push($HoAoA{hash1}[2], "seventh");

The first argument to push should be an actual array and not just an
reference. So you have to dereference it:

push @{ $HoAoA{hash1}[2] }, "seventh";
Thanks! This works. Seems as I have to read a little bit about that :)
 

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

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top