chakram88 has asked for the wisdom of the Perl Monks concerning the following question:
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)
Results:#!/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; }
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Location of Conditional test effects results
by ferreira (Chaplain) on Apr 10, 2007 at 19:43 UTC | |
by chakram88 (Pilgrim) on Apr 10, 2007 at 20:24 UTC | |
|
Re: Location of Conditional test effects results
by shmem (Chancellor) on Apr 10, 2007 at 19:58 UTC | |
by chakram88 (Pilgrim) on Apr 10, 2007 at 20:23 UTC | |
by ikegami (Patriarch) on Apr 10, 2007 at 21:43 UTC | |
by chakram88 (Pilgrim) on Apr 11, 2007 at 01:16 UTC | |
|
Re: Location of Conditional test effects results
by graff (Chancellor) on Apr 11, 2007 at 05:06 UTC |