in reply to Re: Re: loop control
in thread loop control

Your code as posted (almost) works. After modifying it to
#!/usr/bin/perl -w use strict; my $out = '<?xml version="1.0" encoding="utf-8"?>'; my @tmp = split /</, $out; foreach(@tmp) { if (substr($_, 0, 1) eq "?") { print "conditionA matched - looping!\n"; next; } elsif (substr($_, 0, 1) eq "!") { print "conditionB matched\n"; } print "End of loop\n"; }
I get the output
End of loop conditionA matched - looping!
as expected. (Note that the split is returning a list of two scalars, not just one: the 0 characters before the < and the n characters after it.)

Side note: That feels a lot like C. Changing your tests to if (/^\?/) and if (/^!/) would feel perlier. Should work either way, though.

Replies are listed 'Best First'.
Re: Re: Re: Re: loop control
by Galen (Beadle) on May 22, 2002 at 17:34 UTC
    OK - the problem was what you mentioned, that split is returning two scalars. The first 0 character result is passing through the conditional check and messing things up - it's not the xml declaration, as I thought. By the way, I tried to abbreviate how the string $out was populated and forgot it had quotes in it, so code as posted would have failed (as you and others noticed). The loop works now - thanks much you guys rock! As for the use of substr instead of regex.. well I was mostly doing it for consistency, because substr works so well for isolating pieces of the xml elements. But, efficiency is more important so I'll change it - thanks for the suggestion :)