Newbie Question about Hashes

D

Dennis Russo

A little new to perl (and maybe hashes). I have two seperate arrays
that are in order, and I would like to create a hash table to combine
the two (ie $hash{arrayOne[5]}=arrayTwo[5]). I'm not sure why the
following code does not work (it only prints two blannk lines):

%hash = ();

for($i=0; $i<10; $i++) {
$a[$i]=$i;
}

for($i=10; $i<20; $i++) {
$b[$i]=$i;
}

$i=0;
foreach (@a) {
$hash{$_}=$b[$i];
$i++;
}

print "$hash{$a[0]}\n";
print "$hash{$a[1]}\n";

I am expecting the following output:
10
11

Any thoughts or inputs would be greatly appreciated!!


Thanks,
dr
 
G

Gary E. Ansok

A little new to perl (and maybe hashes). I have two seperate arrays
that are in order, and I would like to create a hash table to combine
the two (ie $hash{arrayOne[5]}=arrayTwo[5]). I'm not sure why the
following code does not work (it only prints two blannk lines):

I would add

use strict;
use warnings;

here, even for a short script like this.
%hash = ();

for($i=0; $i<10; $i++) {
$a[$i]=$i;
}

Nothing really wrong with this but it would be more commonly written as

for my $i (0..9) {
$a[$i] = $i;
}

or even

$a[$_] = $_ foreach (0..9);
for($i=10; $i<20; $i++) {
$b[$i]=$i;
}

Same comments apply here.
$i=0;
foreach (@a) {
$hash{$_}=$b[$i];
$i++;
}

Here's your problem -- the first time through the loop,
$_ is $a[0], which is 0, and $i is 0. So the assignment
works out to

$hash{0}=$b[0]

but you didn't assign a value to $b[0], only to $b[10] through $b[19],
so $hash{0} gets the undefined value. Perhaps that first line in
this clip should be "$i = 10"?
print "$hash{$a[0]}\n";
print "$hash{$a[1]}\n";

These do indeed work out to $hash{0} and $hash{1}, but as I
mentioned before, these don't have a defined value. If you'd
had "use warnings" or -w enabled, you would have gotten a message
about an undefined value (whether that would help in this case
I couldn't say). So just the newline is printed.

Gary
 
B

Bob Walton

Dennis said:
A little new to perl (and maybe hashes). I have two seperate arrays
that are in order, and I would like to create a hash table to combine
the two (ie $hash{arrayOne[5]}=arrayTwo[5]). I'm not sure why the
following code does not work (it only prints two blannk lines):

%hash = ();

for($i=0; $i<10; $i++) {
$a[$i]=$i;
}

for($i=10; $i<20; $i++) {
$b[$i]=$i;

-------^^
That should probably be $b[$i-10]=$i; -- as written, you are defining
$b[10] through $b[19], which probably isn't what you really wanted.

$i=0;
foreach (@a) {
$hash{$_}=$b[$i];


Note that $b[0] through $b[9] are reference here, and have undef as
their value.

$i++;
}

print "$hash{$a[0]}\n";
print "$hash{$a[1]}\n";

I am expecting the following output:
10
11

Any thoughts or inputs would be greatly appreciated!!


Thanks,
dr

Why don't you do:

use warnings;
use strict;
my %hash = ();
my @a=(0..9);
my @b=(10..19);
@hash{@a}=@b;
use Data::Dumper;
print Dumper(\%hash);

That's a lot more Perlish and it also works.
 
T

Tad McClellan

Dennis Russo said:
I have two seperate arrays
that are in order, and I would like to create a hash table to combine
the two (ie $hash{arrayOne[5]}=arrayTwo[5]).


my %hash;
@hash{ @arrayOne } = @arrayTwo; # a "hash slice", see perldata.pod
 
T

Tore Aursand

I have two seperate arrays that are in order, and I would like to create
a hash table to combine the two [...]

How about this one?

for ( 0 .. $#array1 ) {
$hash{ $array1[$_] } = $array2[ $_ ];
}

You're warned, though; You should actually for() through the array with
the most elments (if the arrays have different number of elements). That
way you can set the value to something default if there's no element at
the given index in $array2.
 

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

Similar Threads


Members online

Forum statistics

Threads
474,266
Messages
2,571,075
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top