help with arrays to hash

Discussion in 'Perl Misc' started by jm, Jan 29, 2004.

  1. jm

    jm Guest

    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
     
    jm, Jan 29, 2004
    #1
    1. Advertising

  2. 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.
    >
    >
    > 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;

    --
    David Wall
     
    David K. Wall, Jan 29, 2004
    #2
    1. Advertising

  3. jm

    jm Guest

    "David K. Wall" <> wrote in message
    news:Xns947FA5E32C775dkwwashere@216.168.3.30...
    > 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.
    > >
    > >
    > > 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;
    >
    > --
    > 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
     
    jm, Jan 29, 2004
    #3
  4. 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
     
    John W. Krahn, Jan 30, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Philipp
    Replies:
    21
    Views:
    1,173
    Philipp
    Jan 20, 2009
  2. rp
    Replies:
    1
    Views:
    562
    red floyd
    Nov 10, 2011
  3. Adam Akhtar
    Replies:
    5
    Views:
    674
    Adam Akhtar
    Mar 25, 2008
  4. Älphä Blüë

    Hash of Hash of Arrays Question

    Älphä Blüë, Jul 18, 2009, in forum: Ruby
    Replies:
    5
    Views:
    681
    Älphä Blüë
    Jul 18, 2009
  5. Tore Aursand
    Replies:
    3
    Views:
    570
    Anno Siegel
    Sep 16, 2003
Loading...

Share This Page