Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > help with arrays to hash

Reply
Thread Tools

help with arrays to hash

 
 
jm
Guest
Posts: n/a
 
      01-29-2004
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


 
Reply With Quote
 
 
 
 
David K. Wall
Guest
Posts: n/a
 
      01-29-2004
jm <(E-Mail Removed)> wrote:

> 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:umper;
print Dumper \%info;

--
David Wall
 
Reply With Quote
 
 
 
 
jm
Guest
Posts: n/a
 
      01-29-2004

"David K. Wall" <(E-Mail Removed)> wrote in message
news:Xns947FA5E32C775dkwwashere@216.168.3.30...
> jm <(E-Mail Removed)> wrote:
>
> > 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:umper;
> print Dumper \%info;
>
> --
> David Wall


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


 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      01-30-2004
jm wrote:
>
> 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
--
use Perl;
program
fulfillment
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash of Hash of Arrays Question Älphä Blüë Ruby 5 07-18-2009 07:36 PM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
hash of arrays - appending to one of the arrays Adam Akhtar Ruby 5 03-25-2008 11:53 PM
Sort a hash based on values in the hash stored as arrays of hashes Tore Aursand Perl Misc 3 09-16-2003 10:14 AM



Advertisments