So I have a hash that is loaded w/ values. The keys are numerical values like 1, 2, 3, etc... The values are just names. The keys correspond to columns #'s and the value is what will go into the cell. But when I run the code I get the error "Can't Use an undefined value as a HASH reference" related to the code: $worksheet->Cells($current_row, $key)->{Value}=$hash{$key}; So in the loop it prints the key correctly and prints the value correctly, but then bomb ats the next line. If I remove the $key value from the excel call and replace it with the number that $key printed it runs fine (of course the keys are not the same so this doesn't work, but tell me $key is the problem). So what can I do to fix this? I just find this very strange. I can print $key and it prints the correct numerical value, but when I try to use $key to write to excel I get an error? So how do I get the excel call to take or recognize my $key value? Not sure if this is something I'm doing incorrectly w/ the HASH key/value or if there is some way I can modify the hash key so its just a normal value that I can put into the excel write call. Also, the $current_row is ok, its auto incremented in another part of the code and I print that out and its ok. I know its something to do w/ the $key value, just don't know what or where to start.
$worksheet = $workbook->Worksheets("Worksheet"); foreach my $key (sort keys %hash){ print "Key: $key\nValue: $hash{$key}\n"; $worksheet->Cells($current_row, $key)->{Value} = $hash{$key}; }
SOLUTION: Adding a new variable and setting that variable equal to $key + 0....
$worksheet = $workbook->Worksheets("Worksheet"); foreach my $key (sort keys %hash){ print "Key: $key\nValue: $hash{$key}\n"; $column = $key + 0; $worksheet->Cells($current_row, $column)->{Value} = $hash{$key}; }

In reply to Can't Use an undefined value as a HASH reference when passing HASH key into Excel OLE call. by kgnickl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.