Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Newbie Question about Hashes

Reply
Thread Tools

Newbie Question about Hashes

 
 
Dennis Russo
Guest
Posts: n/a
 
      11-13-2003
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
 
Reply With Quote
 
 
 
 
Gary E. Ansok
Guest
Posts: n/a
 
      11-13-2003
In article <(E-Mail Removed) >,
Dennis Russo <(E-Mail Removed)> wrote:
>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
--
'Normal' is just a setting on the washing machine.
 
Reply With Quote
 
 
 
 
Bob Walton
Guest
Posts: n/a
 
      11-13-2003
Dennis Russo wrote:

> 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:umper;
print Dumper(\%hash);

That's a lot more Perlish and it also works.
--
Bob Walton
Email: http://bwalton.com/cgi-bin/emailbob.pl

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      11-13-2003
Dennis Russo <(E-Mail Removed)> wrote:

> 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


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Tore Aursand
Guest
Posts: n/a
 
      11-13-2003
On Wed, 12 Nov 2003 16:56:12 -0800, Dennis Russo wrote:
> 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.


--
Tore Aursand <(E-Mail Removed)>
 
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
How to make an array of hashes to a single array with all thevalues of these hashes ? kazaam Ruby 12 09-13-2007 01:30 PM
using hashes as keys in hashes Steven Arnold Ruby 3 11-23-2005 03:25 PM
Hash of hashes, of hashes, of arrays of hashes Tim O'Donovan Perl Misc 5 10-28-2005 05:59 AM
Question about hashes of hashes djacober Perl Misc 10 07-25-2005 10:01 AM
Hashes of Hashes via subs Ben Holness Perl 8 10-08-2003 06:57 AM



Advertisments