help with arrays to hash

J

jm

Looking for guidance.
I have multiple arrays with data, and each element is directly associated
with the
same element number in the other arrays. I want to be able to access the
information
for each company by the account number in @acctnum.
Looks like I should be able to use hash of hash, which I don't quite
understand.
I Looked at perldsc and it doesn't make sense to me how to accomplish this.
Tried various ways of using my %HOH and always get errors.
If this can be done, please point me in the right direction, or explain.


use strict;
use warnings;

my @acctnums = ("000101", "000102", "000103", "000104", "000105");

my @companies = ("Flower Mart", "Amigos", "Lacy's", "Good Dairy", "Western
Wear");

my @addresses = ("1100 Fashion Drive", "544 Grand Ave.", "636 Miller Ave.",
"1536 Ocean View", "111 Camino Dr.");

my @cities = ("San Francisco", "San Mateo", "Belmont", "San Francisco",
"Burlingame");

my @phonenums = ("415-555-1234", "650-555-1234", "650-555-4567",
"415-555-9876", "650-555-5432");

my %acctnum = @acctnums;
my %company = @companies;
my %address = @addresses;
my %city = @cities;
my %phonenum = @phonenums;
my %HoH = (
acctnum => {company => address},
{address => city},
{city => phonenum}
);

thanks in advance
jm
 
D

David K. Wall

jm said:
Looking for guidance.
I have multiple arrays with data, and each element is directly
associated with the same element number in the other arrays. I want to
be able to access the information for each company by the account
number in @acctnum. Looks like I should be able to use hash of hash,
which I don't quite understand.
I Looked at perldsc and it doesn't make sense to me how to accomplish
this. Tried various ways of using my %HOH and always get errors.
If this can be done, please point me in the right direction, or explain.


use strict;
use warnings;

my @acctnums = ("000101", "000102", "000103", "000104", "000105");

my @companies = ("Flower Mart", "Amigos", "Lacy's", "Good Dairy",
"Western Wear");

my @addresses = ("1100 Fashion Drive", "544 Grand Ave.", "636 Miller
Ave.",
"1536 Ocean View", "111 Camino Dr.");

my @cities = ("San Francisco", "San Mateo", "Belmont", "San Francisco",
"Burlingame");

my @phonenums = ("415-555-1234", "650-555-1234", "650-555-4567",
"415-555-9876", "650-555-5432");

If you want to key on account number, try this:

my %info;
for my $i (0..$#acctnums) {
$info{$acctnums[$i]} = {
company => $companies[$i],
address => $addresses[$i],
city => $cities[$i],
phone => $phonenums[$i],
};
}

This creates a hash for which the keys are account numbers and the values
are references to anonymous hashes with their own key/value pairs,
informally called a hash of hashes. I hope that helps in making more sense
out of perldsc. (Your original code looks like you would have got there
eventually.)

Then if you want to see the entire data structure,

use Data::Dumper;
print Dumper \%info;
 
J

jm

David K. Wall said:
jm said:
Looking for guidance.
I have multiple arrays with data, and each element is directly
associated with the same element number in the other arrays. I want to
be able to access the information for each company by the account
number in @acctnum. Looks like I should be able to use hash of hash,
which I don't quite understand.
I Looked at perldsc and it doesn't make sense to me how to accomplish
this. Tried various ways of using my %HOH and always get errors.
If this can be done, please point me in the right direction, or explain.


use strict;
use warnings;

my @acctnums = ("000101", "000102", "000103", "000104", "000105");

my @companies = ("Flower Mart", "Amigos", "Lacy's", "Good Dairy",
"Western Wear");

my @addresses = ("1100 Fashion Drive", "544 Grand Ave.", "636 Miller
Ave.",
"1536 Ocean View", "111 Camino Dr.");

my @cities = ("San Francisco", "San Mateo", "Belmont", "San Francisco",
"Burlingame");

my @phonenums = ("415-555-1234", "650-555-1234", "650-555-4567",
"415-555-9876", "650-555-5432");

If you want to key on account number, try this:

my %info;
for my $i (0..$#acctnums) {
$info{$acctnums[$i]} = {
company => $companies[$i],
address => $addresses[$i],
city => $cities[$i],
phone => $phonenums[$i],
};
}

This creates a hash for which the keys are account numbers and the values
are references to anonymous hashes with their own key/value pairs,
informally called a hash of hashes. I hope that helps in making more sense
out of perldsc. (Your original code looks like you would have got there
eventually.)

Then if you want to see the entire data structure,

use Data::Dumper;
print Dumper \%info;

Yes, that does exactly want I want!
I may have gotten there some day, I was starting to think towards a loop,
but...
You have done it.
Many thanks
jm
 
J

John W. Krahn

jm said:
Looking for guidance.
I have multiple arrays with data, and each element is directly associated
with the
same element number in the other arrays. I want to be able to access the
information
for each company by the account number in @acctnum.
Looks like I should be able to use hash of hash, which I don't quite
understand.
I Looked at perldsc and it doesn't make sense to me how to accomplish this.
Tried various ways of using my %HOH and always get errors.
If this can be done, please point me in the right direction, or explain.

You probably want somthing like this:

use strict;
use warnings;

my @acctnums = ( '000101', '000102',
'000103', '000104', '000105' );
my @companies = ( 'Flower Mart', 'Amigos',
'Lacy\'s', 'Good Dairy', 'Western Wear' );
my @addresses = ( '1100 Fashion Drive', '544 Grand Ave.', '636 Miller
Ave.', '1536 Ocean View', '111 Camino Dr.' );
my @cities = ( 'San Francisco', 'San Mateo',
'Belmont', 'San Francisco', 'Burlingame' );
my @phonenums = ( '415-555-1234', '650-555-1234',
'650-555-4567', '415-555-9876', '650-555-5432' );

my %HoH;

@HoH{ @acctnums } = map { Company => $companies[ $_ ],
Address => $addresses[ $_ ],
City => $cities[ $_ ],
Phone => $phonenums[ $_ ],
}, 0 .. $#companies;



John
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top