PDA

View Full Version : Hash of arrays of hashes


tom123
03-24-2006, 07:21 PM
Im trying to create a hash of arrays of hashes i.e a table which contains an array of fields, with each field containing a reference to an order scalar value ("ascending" or "descending"). I dont think the code below is correct, and how do i access the scalar value for each field?

Ive changed it around and pretty sure its creating the structure. but i can seem to dereference it properly to get the field name.

# Get the current session data
%my_data = %{$session->param('my_data')};

$order = "descending";

# Create hash of arrays of hashes
foreach my $table(keys %my_data){
foreach my $field(@{$my_data{$table}}){
$my_possible_sorts{$table} = [ {$field => $order} ] ;
print %(@{my_possible_sorts{$table}}}; #Please help with this line
}
}


Thanks in advance

KevinADC
03-26-2006, 07:43 AM
Looks to me like you can create a hash of hashes, no need for a hash of arrays of hashes unless there is something else going on elsewhere in the script. Here is a hash of hashes:

$order = 'descending';
# Create hash of hashes
foreach my $table (keys %my_data){
foreach my $field (@{$my_data{$table}}) {
$my_possible_sorts{$table} = {$field => $order};
print $my_possible_sorts{$table}{$field}; #Please help with this line
}
}

FishMonger
03-26-2006, 08:40 AM
foreach my $field (@{$my_data{$table}})
Kevin, your example is de referencing a hash of arrays.

These examples are a little more complete.

Hashes of Arrays
http://www.unix.org.ua/orelly/perl/prog3/ch09_02.htm

Hashes of Hashes
http://www.unix.org.ua/orelly/perl/prog3/ch09_04.htm

KevinADC
03-26-2006, 09:51 AM
Kevin, your example is de referencing a hash of arrays.


Well, it's not really my example, I was just using what the OP had posted and don't really know exactly what his data structure is so my suggestion could be erroneous. I have that CD (Perl BookShelf References), but thanks for the link, even though it is to a pirated copy of the CD. :(

FishMonger
03-26-2006, 11:10 AM
thanks for the link, even though it is to a pirated copy of the CD.How do you know that it's a "pirated copy"? Do you work for O'Reilly? It very possibly and most likely is a legitimate mirror.

Why would you copy/paste the code that the Tom posted but preference it with "Here is a hash of hashes:" as if it were you own example? It's interesting to note that you didn't know what type of data structure your were posting, but still made the claim that it was a hash of hashes.

KevinADC
03-26-2006, 08:42 PM
How do you know that it's a "pirated copy"? Do you work for O'Reilly? It very possibly and most likely is a legitimate mirror.

Why would you copy/paste the code that the Tom posted but preference it with "Here is a hash of hashes:" as if it were you own example? It's interesting to note that you didn't know what type of data structure your were posting, but still made the claim that it was a hash of hashes.

I thought the question was how to create a new structure based on %my_data. The OP wanted a hash of array of hash, which he was doing:

$my_possible_sorts{$table} = [ {$field => $order} ] ;

I didn't see a need for the array just to hold one key/value pair so mine was creating a hash of hash:

$my_possible_sorts{$table} = {$field => $order};

in both instances %my_possible_sorts appears to be being built from %my_data, which appears to be a hash of arrays, so is dereferenced as such (by the OP) and then converted into a new data set. I hope that clears it up.

KevinADC
03-27-2006, 01:31 AM
How do you know that it's a "pirated copy"? Do you work for O'Reilly? It very possibly and most likely is a legitimate mirror.


What could it be a mirror to? As far as I know O'Reilly does not allow their reference materials to be uploaded in whole, and that site has many O'Reilly reference materials uploaded as well as those of many more publishers. I will let you know if and when O'Reilly publishing returns my email.

KevinADC
03-27-2006, 08:12 PM
Well, O'Reilly got back promptly, and this is the reply I recieved:


Hi Kevin,

The site you reference is infringing upon O'Reilly copyrights.

If you wish to access authorized on line versions of the same material (as
well as material from other publishers), please check out
http://safari.oreilly.com/.

We appreciate your support.
Legal Department
O'Reilly Media, Inc.
Sebastopol, CA


This is the email I sent:


I came across this link on a forum:

***link removed to pirated material***

Can you please let me know if this is legal or illegal use of O'Reilly
material. If legal I would like to use it as a source of reference
material online, if it's not legal I would like to know so I can avoid
using it and hopefully the site can be shutdown and/or the illegal
materials removed.

Thank you,
Mr Kevin *********


O'Reilly can be contacted at:

infringement@oreilly.com

if ever there were questions about O'Reilly materials online.