On 24 May 2017 at 15:20, ToddAndMargo <toddandma...@zoho.com> wrote:
>
>
> This is what I came up with.  I found that `next` did not serve me
> well, so i just used a tag.
>
> Thank you all for the help.  I had a bit of a time wrapping
> my head around `next` there for a while.
>
> -T
>
> <code>
> #!/usr/bin/env perl6
>
> use strict;
>
> my @Data = '<TASK type="D">Mission D',
>            '   <NAME type="P">Sol Wheat</NAME>',
>            '   <NAME type="P">Ted Moon</NAME>',
>            '</TASK>';
> # for @Data -> $Line { say "$Line"; }
>
> my $TaskTag = 0;
> for @Data -> $Line {
>    if $Line.contains( "TASK type" ) { $TaskTag = 1; }
>
>    if $Line.contains( "NAME type" ) {
>       ( my $Name = $Line) ~~ s/.*?\>//;
>       $Name ~~ s/\<.*//;
>       say $Name;
>    } else { $TaskTag = 0; }
>
> }
> </code>
>

I'm a rank beginner p6 programmer so I'm sure my code can be improved.

However, your code does not look like it will do what you want if you have
multiple TASKs

What about something like:
my @Data =
     '<TASK type="D">Mission D',
           '   <NAME type="P">Sol Wheat</NAME>',
           '   <NAME type="P">Ted Moon</NAME>',
     '</TASK>',
     '<TASK type="C">Mission C',
           '   <NAME type="P">Jim Kirk</NAME>',
           '   <NAME type="P">Mr Spock</NAME>',
     '</TASK>',
;

my $in-TASK = "";

for @Data -> $Line {
    if $Line.contains( "TASK type" ) {
        $Line ~~ m/ 'TASK' .*? '>' $<Task>=( .* ) /;
        $in-TASK = $<Task>;
        next;
    }
    if $Line.contains( "/TASK" ) {
        $in-TASK = "";
        next;
    }
    if $in-TASK ne "" && $Line.contains( "NAME type" ) {
        $Line ~~ m/ 'NAME' .*? '>' $<Name>=( .*? ) '<' /;
        say $in-TASK, $<Name>;
    }
}


-- 
Norman Gaywood, Computer Systems Officer
School of Science and Technology
University of New England
Armidale NSW 2351, Australia

ngayw...@une.edu.au  http://turing.une.edu.au/~ngaywood
Phone: +61 (0)2 6773 2412  Mobile: +61 (0)4 7862 0062

Please avoid sending me Word or Power Point attachments.
See http://www.gnu.org/philosophy/no-word-attachments.html

Reply via email to