in reply to Re: Using array value (from array ref) as hash key??
in thread Using array value (from array ref) as hash key??

Thanks for the reply and the clarification, I understand what you're saying now. :)

I remembered something I had problems with before and tried that and now I'm even more puzzled...(I've had problems with things not working due to whitespace issues so I fairly routinely use a trim function I got from the cookbook), I tried it with this problem and lo and behold it works.

my $months = $dbh->selectall_arrayref("select to_char(last_day(add_mon +ths('14-May-2004', (rownum-1))),'MON-RRRR') dt from all_objects where rownum <= months_between(sysdate,'31-May-2004')+1"); my $month; my $monthhash; my $i = 0; # populate month hash with zero's for now (this may need to be done la +ter) foreach $month (@$months) { $monthhash{@$month[0]} = $i; # print "pop @$month[0] $i\n"; $i++; } for($i=0; $i<$#$months; $i++) { print "hope 1 @{$months->[$i]}\n"; print "hope 2 $monthhash{trim(@{$months->[$i]})}\n"; print "hope 2A $monthhash{@{$months->[$i]}}\n"; } sub trim { my @out = @_; for (@out) { s/^\s+//; #trim left s/\s+$//; #trim right } return @out == 1 ? $out[0] #only one to return : @out; #or many }

Results. I added an incrementing value to the hash when I created it to make sure that I was indeed getting the correct value - As far as I can see the only difference between hope 2 & hope 2A is the trim call, yet one works and the other doesn't. I don't know why this should make any difference but it is now working.

I was wondering whether it had anything to do with the array only having one element. I started with the simple query so I'll be using your notation later on. If thats not it, then I have no idea why the trim should make any difference.

hope 1 MAY-2004 hope 2 0 hope 2A hope 1 JUN-2004 hope 2 1 hope 2A hope 1 JUL-2004 hope 2 2 hope 2A hope 1 AUG-2004 hope 2 3 hope 2A hope 1 SEP-2004 hope 2 4 hope 2A

Replies are listed 'Best First'.
Re^3: Using array value (from array ref) as hash key??
by ikegami (Patriarch) on May 25, 2007 at 06:08 UTC
    $key = @...[...]; <--- Wrong ^ ^ It may work because Perl is "smart", | | but Perl doesn't always guess correctly. +------+ $key = $...[...]; <--- Right ^ ^ | | +------+

    A hash key is a scalar, so you need a "$ expression".

    # Wrong: print "hope 1 @{$months->[$i]}\n"; print "hope 2 $monthhash{trim(@{$months->[$i]})}\n"; print "hope 2A $monthhash{@{$months->[$i]}}\n"; # Right: print "hope 1 $months->[$i][0]\n"; print "hope 2 $monthhash{trim($months->[$i][0])}\n"; print "hope 2A $monthhash{$months->[$i][0]}\n";

    You're assuming an array of one element (@array) is interchangeable with the element ($array[0]). That's not always the case. In fact, you should assume it's not the case.

    By the way, @out == 1 is a bug. You should replace @out == 1 with wantarray. You'll get some interesting results when you do.

      Thanks for your patience Ikegami, I get it now. (finally) :)

      Thanks for the clarification about @out == 1 is a bug, I got that straight from the perl cookbook as a solution (1.19). But I can see that wantarray would be better.