Congratulations on fixing the problem per your update to the root node. You have taught me something I didn't know about hash keys. In my experience, Perl respects integers, but clearly hash keys are coerced into something different. I'm a little nervous, though, about coercing by adding zero. If the key has been changed into a floating point number, you might conceivably get 0.9999 (however many) instead of 1. Excel has to have an integer as a row or column number, and if that coercion is done by int or something comparable, you might get an "out by one" error. Although we have tried row 1 and it seems to work, it remains possible that in some system you will attempt to access row or column zero and get a run-time error. I therefore prefer the approach I have used in the following code. As an aside, $row++ is more Perlish and less typing than your way, but your way works fine.

use strict; use warnings; use diagnostics; use Win32::OLE; my $xl = Win32::OLE->new('Excel.Application'); $xl->{Visible} = 1; my $wb = $xl->Workbooks->Add(); for (2..$wb->Sheets->{Count}) { $wb->Sheets(2)->Delete; } my $worksheet = $wb->Sheets(1); my %hash = (1 => 'One', 5 => 'Five', 10=> 'Ten'); my $row = 0; foreach my $key (sort keys %hash){ $row++; $worksheet->Cells($row, int($key + .5))->{Value} = $hash{$key}; }

Regards,

John Davies


In reply to Re^3: Can't use an undefined value as a hash reference when passing hash key into Excel OLE call. by davies
in thread 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.