Oh revered monks, I come seeking knowledge and wisdom.

Quick description of my 'issue:

I can take the same block of code, and move one line (a conditional operator) from one point to another, and I end up with a different result.

My Data structure is an AoH, where a Hash element 'could' be an array. The conditonal is testing for the existance of that secondary array. I loop over the AoH.

With the conditional operator assignment at the top of my loop, I get the expected results based on the test in the conditional. If I move the conditional assignment to the end of my loop (no data changes within the loop), I get responses as if the conditional were always true.

Code whittled down (line marked with <------ is the offending little bugger)

#!/usr/bin/perl -l use warnings; use strict; my $records = [ { 'services' => [], 'zone_record_id' => '1', }, { 'services' => [ { 'status' => 'OK', 'id' => '0', 'class' => 'Primary' } ], 'zone_record_id' => '2', }, { 'services' => [ { 'status' => 'OK', 'id' => '1', 'class' => 'Editable' } ], 'zone_record_id' => '4', }, { 'services' => [ { 'status' => 'OK', 'id' => '1', 'class' => 'Secondary' } ], 'zone_record_id' => '5', }, ]; my $lbr = "\n=============\n"; foreach my $record (@$records) { my $editor; print $lbr, "Record: $record->{'zone_record_id'}"; # Conditional array defined check first $editor = (@{$record->{'services'}})? 'service_edit' : 'plain_edit +'; # <--- $editor = "no editor" if $record->{'services'}[0]{'class'} eq 'Pri +mary'; $editor = "no editor" if $record->{'services'}[0]{'class'} eq 'Sec +ondary'; # place here for 'Try to define all 'no editor' situations first print $editor; }
Results:
Conditionaly check for defined array first: ============= Record: 1 plain_edit ============= Record: 2 no editor ============= Record: 4 service_edit ============= Record: 5 no editor Try to define all 'no editor' situations first: ============= Record: 1 service_edit ============= Record: 2 service_edit ============= Record: 4 service_edit ============= Record: 5 service_edit

Yes, I know that the eq test is putting up 'undefined' warnings on those $records where the secondary array is not defined. I deal with it in my full code, this is just "narrowed down to the least amount of code to reproduce the problem".

P.S. I had a difficult time coming up with an appropriate title, so any suggestions welcome


In reply to Location of Conditional test effects results by chakram88

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.