Your core problem is here:

if (exists $colD{$valA}) { ... elsif (! exists $colD{$valA}) { ... }

Clearly, if exists $colD{$valA} is false, then its negation must be true:

if (exists $colD{$valA}) { ... else { ... }

However, as you never set $colD{$valA}, the condition will always be false and the else block will only ever be entered.

There are lots of other issues with this code. Here's a non-exhaustive list in no particular order.

Let Perl tell you when you've done something wrong

Add use warnings; - right after use strict; is a good place. If you're having trouble understanding the messages, add use diagnostics; for more verbose messages. Relevant documentation: warnings and diagnostics.

... use strict; use warnings; use diagnostics; ...
Lay out your code in a consistent fashion

Your indentations appear almost totally arbitrary and your use of whitespace seems quite erratic. This makes reading your code difficult but it doesn't need to be. Please read: perlstyle.

Use more meaningful variable names

There's a number of places where some sort of naming convention would have helped you. For instance, you use three variables for worksheet objects: $sheet, $worksheet1 and $worksheet. Except where you declare and assign these, they don't convey a lot of meaning. Perhaps $sheet_in and $sheet_out would have been more informative. And yes, that is two new names for three existing variables: another bug in your code that I just found!

Use lexical variables for filehandles

Instead of open FILE ..., use something like open my $whatever_fh .... See open for a discussion of this.

Avoid pointless repetition of code

You have the identical statement my $valA = $sheet->{Cells}[$row][0]->{Val}; in both blocks of your if-else code. This would be better as:

my $valA = $sheet->{Cells}[$row][0]->{Val}; if (exists $colD{$valA}) { ...

That should be enough for you to get your code working. If you need to ask further questions, please first read: How do I post a question effectively?

-- Ken


In reply to Re: struggling with spreadsheet::WriteExcel and writing columns by kcott
in thread struggling with spreadsheet::WriteExcel and writing columns by trickyq

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.