Array if first and second line in an array match don't print

mikeysmailbox1
mikeysmailbox1 used Ask the Experts™
on
I have and array and in the array I need to not print the following.

Dont print if line 1 = <ON CODE
Dont print if line 2 = </ON>

but print if they do not follow each other

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Try this:


Try:
$\ = "\n";
while (@array)
{
    my $a = shift(@array);
    if($a =~ /<ON CODE/)
    {
       my $b = shift(@array);
       unless($b =~ m{</ON>})
       {
             print "$a","\n","$b";
       }
    }
    else
    {
        print "$a";
    }
}

Open in new window

Another way :

$\ = "\n";

foreach my $i (0..$#array)
{
    next if($array[$i] =~ m{</ON>} and ${array[$i-1]} =~ /<ON CODE/);
    print $array[$i];
}

Open in new window

Or this...

Will it always be explicitly "<ON CODE" and "</ON>" or is that just an example?  I wrote the below for the generic "<XXX\b" and "</XXX>" but it would be easy to change it to be specific.

my $i = 0;
while ($i < @array) {
    if (my ($a) = ($array[$i] =~ m{^\s*<(\w+)\b}) and $array[$i+1] =~ m{^\s*</$a>}) {
        $i += 2;
    } else {
        print $array[$i], "\n";
        $i++;
    }
}

Open in new window


If it should be specifically "<ON CODE" and "</ON>" then just change the if line within the while loop to be:

if ($array[$i] =~ m{^\s*<ON\s+CODE\b} and $array[$i+1] =~ m{^\s*</ON>}) {

Author

Commented:
Thanks

This worked perfect.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial