http://www.perlmonks.org?node_id=1002373

reaper9187 has asked for the wisdom of the Perl Monks concerning the following question:

hi people...
Back again to seek you wisdom and guidance on a particularly weird and frustrating problem... SO here goes...

I'm trying to compare the elements of an array with each cell of an excel sheet and print the one that matches... i know this looks pretty simple .... But i've been facing a weird problem:
when i print the array and the cell individually , i can easily make out the similar values,.. but when i compare the values,the code doesnt seem to realize it .. it jus treats it as a different element(eg it treats 1 and 1 as diff). I've been using strict , warnings and diagnostics.. but none seem to find any error...
i've tested the rest of the code.. the trouble some part is given below:
foreach $ray(@cellr) { if($ray eq $worksheet->{Cells}[$row][1]->{Val} ) { print "$ray :yes\n"; } else { next; } }
Easy enough right ..??? Now there is a another way(comparatively difficult and inefficient ) by means of pattern match but i dont want to go there.. Help please..!! i dnt think its a problem with the files coz i am able to read and print the values perfectly.. its the comparison (for some reason) thats starting to bug me nw ...Any ideas what can cause such a problems ?? THanks.!!

Replies are listed 'Best First'.
Re: Weird excel parsing problem
by BrowserUk (Patriarch) on Nov 05, 2012 at 18:54 UTC
    Any ideas what can cause such a problems ??

    You are almost certainly suffering from whitespace differences.

    Try printing both values like this:

    print "'$ray' '$worksheet->{Cells}[$row][1]->{Val}'"; ## Pay special a +ttention to the quotes

    With this quoting, the issue should become obvious.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

      It was a whitespace issue but only in some of the values .. Anyways its working fine now ... thnk you evryone for your prompt replies and valuable guidance .. it because of this that i love this place... :) sometimes the tiniest details matter the most .. Thanks everyone ,..!!!
Re: Weird excel parsing problem
by Kenosis (Priest) on Nov 05, 2012 at 18:45 UTC

    If you're comparing numeric values, did you mean the following (== instead of eq)?

    if($ray == $worksheet->{Cells}[$row][1]->{Val} { ...

      Not always. I found a bug trying to deploy JSON::XS on AIX 7 where '5e1' was deciphered by JSON::XS, and came out to 50.0000000007 or something. On other platforms, it worked fine. And this was basically because there's a bug in AIX 7's libm where rounding happens wrong or something. So anyway, when comparing with ==, they failed to match, but comparing with eq made it work (because perl stringified the number to 50, due to a lower precision).

      So what I'd do here is try to get more information about both $ray and each value and see if there's a mismatch at some higher precision, e.g., using sprintf "%.15f", $ray (and likewise for each value in the worksheet being compared against). That is for == failures. For eq failures, what I learned many many years ago was to put my debug in delimiters. I learned with [...], so something like this: print "ray = [$ray]\n" to see if there are spaces. Even better to use Data::Dumper or something else that will not only use delimiters (often quotes) but also look for non-printables and convert them (e.g., tabs, nuls, etc.).

      Its numbers like ABC123 , HYU785, etc.. when i use '==', it gives an error .. so i knw its not numeric for sure ..

        Well, using eq makes sense with these strings. Are you detecting any whitespaces around the strings which may be interfering with the equality test?