develooper Front page | perl.beginners | Postings from December 2002

Re: populating a hash key using a variable

Thread Previous
Ian Zapczynski
December 11, 2002 08:36
Re: populating a hash key using a variable
Message ID:
Yes, Rob, you are absolutely correct.  Unfortunately, I still lack 
confidence in my coding abilities, so when something doesn't work, I 
seem to assume I just don't know how to do it properly. In reality, I 
just made a dumb mistake, which is partially where this lack of 
confidence comes from.  :)

What I did was closer to this:

my $i = 0;
while (EXPR) {
    $hash{KEY}{$i} = $something;
    # ok, so now let's test that I did this correctly
    print $hash{KEY}{$i};

As you can see, silly me put my print statement AFTER I incremented the 
variable, so of course that key was undefined.  Geez.  

Precisely why I'm still on the "beginners" list.  :)

Thanks James and Rob for helping me find that my error was not in syntax.


Rob Dixon wrote:

>Quite. What you've written looks fine Ian. But you've probably shown us what
>you /meant/ rather than what you've actually coded :-}
>----- Original Message -----
>From: "Kipp, James" <>
>To: "'Ian Zapczynski'" <>; <>
>Sent: Wednesday, December 11, 2002 4:11 PM
>Subject: RE: populating a hash key using a variable
>>>If I do $hash{SOMETHING}{0} = $something;
>>>Then my hash is populated correctly.  I am only not getting
>>>results if I
>>>do $hash{SOMETHING}{$i} = $something.  The key here is how I am using
>>>the variable $i in my hash.
>>I tried this on my machine and it worked fine:
>>my $i;
>>for ( 1..5 ) {
>>       $hash{'KEY'}{$i} = "test$i";
>>print "$hash{'KEY'}{$i}\n";
>>       $i++;
>># prints:
>>To unsubscribe, e-mail:
>>For additional commands, e-mail:

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About