Datastructure
am 15.11.2007 20:19:33 von 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?
Re: Datastructure
am 15.11.2007 20:26:37 von Peter Makholm
Bioperler writes:
> %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
Re: Datastructure
am 15.11.2007 20:28:47 von krahnj
Bioperler wrote:
>
> 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
--
use Perl;
program
fulfillment
Re: Datastructure
am 15.11.2007 20:39:51 von Bioperler
Peter Makholm wrote:
> Bioperler writes:
>
>> %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 :-)
Re: Datastructure
am 16.11.2007 01:42:44 von Tad McClellan
Bioperler wrote:
> Peter Makholm wrote:
>> push @{ $HoAoA{hash1}[2] }, "seventh";
> Thanks! This works. Seems as I have to read a little bit about that :-)
You can do that with:
perldoc perlreftut
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"