Newbie Question about Hashes

Discussion in 'Perl Misc' started by Dennis Russo, Nov 13, 2003.

  1. Dennis Russo

    Dennis Russo Guest

    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
     
    Dennis Russo, Nov 13, 2003
    #1
    1. Advertising

  2. In article <>,
    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):
    >


    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.
     
    Gary E. Ansok, Nov 13, 2003
    #2
    1. Advertising

  3. Dennis Russo

    Bob Walton Guest

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

    That's a lot more Perlish and it also works.
    --
    Bob Walton
    Email: http://bwalton.com/cgi-bin/emailbob.pl
     
    Bob Walton, Nov 13, 2003
    #3
  4. 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 (ie $hash{arrayOne[5]}=arrayTwo[5]).



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


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Nov 13, 2003
    #4
  5. Dennis Russo

    Tore Aursand Guest

    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 <>
     
    Tore Aursand, Nov 13, 2003
    #5
    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. Ben Holness

    Hashes of Hashes via subs

    Ben Holness, Oct 5, 2003, in forum: Perl
    Replies:
    8
    Views:
    582
    Ben Holness
    Oct 8, 2003
  2. Steven Arnold

    using hashes as keys in hashes

    Steven Arnold, Nov 23, 2005, in forum: Ruby
    Replies:
    3
    Views:
    179
    Mauricio Fernández
    Nov 23, 2005
  3. kazaam
    Replies:
    12
    Views:
    292
    Matthias Wächter
    Sep 13, 2007
  4. djacober

    Question about hashes of hashes

    djacober, Jul 24, 2005, in forum: Perl Misc
    Replies:
    10
    Views:
    199
    Ian Wilson
    Jul 25, 2005
  5. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    230
Loading...

Share This Page