hash of arrays...

Discussion in 'Perl Misc' started by preacher37@gmail.com, Mar 26, 2009.

  1. Guest

    for (@some_array) {
    @new_array = &do_someting_elesewhere($_);

    # ceate a hash using an element from the array as a key
    $some_hash{$new_array[2]} = @new_array;
    }

    So, how do I see the n^th element of each array?

    foreach $key (keys %some_hash) {
    print $some_hash{$key}[n^th];
    }

    no workey...

    I've tried a million of those impenetrable variations i.e. @{$some_hash
    [n^th]}, etc. but cant find the right syntax. Any help?
     
    , Mar 26, 2009
    #1
    1. Advertising

  2. ccc31807 Guest

    CODE:
    my @languages = (
    {
    language => 'FORTRAN',
    inventor => 'Backus',
    year => '1954',
    },
    {
    language => 'COBOL',
    inventor => 'Hopper',
    year => '1959',
    },
    {
    language => 'Lisp',
    inventor => 'McCarthy',
    year => '1968',
    },
    {
    language => 'Perl',
    inventor => 'Wall',
    year => '1986',
    },
    {
    language => 'Python',
    inventor => 'van Rossum',
    year => '1989',
    },
    {
    language => 'Ruby',
    inventor => 'Matz',
    year => '1993',
    },
    );

    print "$languages[0]{language} was invented in $languages[0]{year},
    but our favorite, $languages[3]{language} is still the best. Except
    for God, who uses $languages[2]{language}.\n";

    OUTPUT:
    C:\PerlLearn>perl arr_hashes.plx
    FORTRAN was invented in 1954, but our favorite, Perl is still the
    best. Except for God, who uses Lisp.

    C:\PerlLearn>
     
    ccc31807, Mar 26, 2009
    #2
    1. Advertising

  3. ccc31807 Guest

    Maybe it would help if I pasted the whole script, note the for loop.
    CODE:
    my @languages = (
    {
    language => 'FORTRAN',
    inventor => 'Backus',
    year => '1954',
    },
    {
    language => 'COBOL',
    inventor => 'Hopper',
    year => '1959',
    },
    {
    language => 'Lisp',
    inventor => 'McCarthy',
    year => '1968',
    },
    {
    language => 'Perl',
    inventor => 'Wall',
    year => '1986',
    },
    {
    language => 'Python',
    inventor => 'van Rossum',
    year => '1989',
    },
    {
    language => 'Ruby',
    inventor => 'Matz',
    year => '1993',
    },
    );

    print "$languages[0]{language} was invented in $languages[0]{year},
    but our favorite, $languages[3]{language} is still the best. Except
    for God, who uses $languages[2]{language}.\n";

    foreach (my $i = 0; $i < @languages; $i++)
    {
    print "In the array, the reference to the hash is $languages[$i]\n";
    print "$languages[$i]{language} invented by $languages[$i]{inventor}
    in $languages[$i]{year}\n\n";
    }

    OUTPUT:

    C:\PerlLearn>perl arr_hashes.plx
    FORTRAN was invented in 1954, but our favorite, Perl is still the
    best. Except for God, who uses Lisp.
    In the array, the reference to the hash is HASH(0x235288)
    FORTRAN invented by Backus in 1954

    In the array, the reference to the hash is HASH(0x235390)
    COBOL invented by Hopper in 1959

    In the array, the reference to the hash is HASH(0x182f9a8)
    Lisp invented by McCarthy in 1968

    In the array, the reference to the hash is HASH(0x18353b4)
    Perl invented by Wall in 1986

    In the array, the reference to the hash is HASH(0x18353f0)
    Python invented by van Rossum in 1989

    In the array, the reference to the hash is HASH(0x183542c)
    Ruby invented by Matz in 1993


    C:\PerlLearn>
     
    ccc31807, Mar 26, 2009
    #3
  4. <> wrote:

    > Subject: Re: hash of arrays...



    "hash of arrays" is just an over-simplified colloquial term.

    "hash of references to arrays" is the technically correct term.


    > for (@some_array) {
    > @new_array = &do_someting_elesewhere($_);

    ^
    ^

    You should not use ampersand on subroutine calls unless what it means
    (perldoc perlsub) is what you want, and it seldom is.

    If you don't want every array to be the same array, then you
    need a fresh @new_array for each loop iteration:

    my @new_array = do_something_elsewhere($_); # spell it correctly too


    > # ceate a hash using an element from the array as a key
    > $some_hash{$new_array[2]} = @new_array;



    $some_hash{$new_array[2]} = \@new_array;


    > }



    Have a look at the data structure you have built:

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

    Does it look right to you?


    > So, how do I see the n^th element of each array?
    >
    > foreach $key (keys %some_hash) {
    > print $some_hash{$key}[n^th];
    > }
    >
    > no workey...



    That would workey fine if you had actually constructed the data structure
    that you were hoping to construct.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
     
    Tad J McClellan, Mar 27, 2009
    #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,166
    Philipp
    Jan 20, 2009
  2. rp
    Replies:
    1
    Views:
    560
    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