I have a somewhat complex data structure that I want to convert to another somewhat complex data structure. I have already figure out how to do it, in the long non-Perl unlazy way. However, I think there must be some faster way that escapes me. It might be using map, which I still don't really understand (hence I didn't use it here). So without further ado, here goes...the data structures:
# Current data structure $ADDRESS{Last1,First1}{Account} = ""; # Blank $ADDRESS{Last1,First1}{First} = "First1"; $ADDRESS{Last1,First1}{Last} = "Last1"; $ADDRESS{Last1,First1}{Address} = "123 Home St"; $ADDRESS{Last1,First1}{Phone} = "(222) 333-4444"; $ADDRESS{Last1,First1}{Email} = "flast1@this.com"; $ADDRESS{Last1,First1}{Description} = "This\nThat\n"; $ADDRESS{Last2,First2}{First} = "First2"; ... $ADDRESS{Last999,First999}{Account} = ""; # Blank $ADDRESS{Last999,First999}{First} = "First999"; # Desired data structure $ADDRESS[0][0] = ""; # Blank $ADDRESS[0][1] = "First1"; $ADDRESS[0][2] = "Last1"; $ADDRESS[0][3] = "123 Home St"; $ADDRESS[0][4] = "(222) 333-4444"; $ADDRESS[0][5] = "flast1@this.com"; $ADDRESS[0][6] = "This\nThat\n"; $ADDRESS[1][0] = ""; # Blank $ADDRESS[1][1] = "First2"; ... $ADDRESS[998][0] = ""; # Blank $ADDRESS[998][1] = "First999"; ...
My current code maps everything the long way:
my $count = 0; foreach my $name (keys %ADDRESS) { $row[$count][0] = ""; $row[$count][1] = $ADDRESS{$name}{First}; $row[$count][2] = $ADDRESS{$name}{Last}; $row[$count][3] = $ADDRESS{$name}{Address}; $row[$count][4] = $ADDRESS{$name}{Phone}; $row[$count][5] = $ADDRESS{$name}{Email}; $row[$count][6] = $ADDRESS{$name}{Description}; $count++; }
There must be a prettier/easier/more Perlish way of doing this. The ultimate goal is to get it to be an AoA because I have an existing script that enters data into an SQL database using the AoA structure used above. Aside from using ORM in the future, any thoughts?
In reply to Converting a HoH to an AoA by madbombX
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |