hash of arrays...

P

preacher37

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?
 
C

ccc31807

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>
 
C

ccc31807

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>
 
T

Tad J McClellan

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.
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top