Hello ravi45722,

This code snippet has a precedence problem:

foreach my $sub_user_error (@db_user_errors) { $worksheet->write( $row, $col, $db_error_data{$hour}{'Submissio +n_user_error'}{$sub_user_error} or 0, $number); $col++; }

If you look at the table in perlop#Operator-Precedence-and-Associativity, you’ll see that logical or has a lower precedence than the comma operator. So if the hash element contains a false value, the expression inside the call to write evaluates to 0, $number, which in turn evaluates to $number — which is not what you want.

The fix is to use the higher-precedence version of logical or, namely ||:

#! perl use strict; use warnings; use feature qw( say ); my $hour = 12; my $error = 'Submission_user_error'; my $sub_user_error = 3; my $number = 42; my %db_error_data; $db_error_data{$hour}{$error}{$sub_user_error} = 0; say 'With ||:'; say $db_error_data{$hour}{$error}{$sub_user_error} || 0, $number; say 'With or:'; say $db_error_data{$hour}{$error}{$sub_user_error} or 0, $number;

Output:

22:40 >perl 1619_SoPW.pl Useless use of private variable in void context at 1619_SoPW.pl line 1 +7. With ||: 042 With or: 0 22:40 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: Need help with Excel::Writer::XLSX in using 'or' by Athanasius
in thread Need help with Excel::Writer::XLSX in using 'or' by ravi45722

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.