Zombie has asked for the wisdom of the Perl Monks concerning the following question:

@achievement = ('level A1', 'level A2', 'level A3', 'level A4', 'level A5', 'level B1' , 'level B2', 'level B3', 'level B4', 'level B5', 'level C1' , 'level C2', 'level C3', 'level C4', 'level C5 +', ); while (@achievement) { my $level = shift @achievement; push (@result, $level); print "Result: @result\n"; }

For the above code, how do I modify the code to wipe out the value of @level when it hit "level 5" ? I want the output to be like

Result: Level A1 Result: Level A1 Level A2 Result: Level A1 Level A2 Level A3 Result: Level A1 Level A2 Level A3 Level A4 Result: Level A1 Level A2 Level A3 Level A4 Level A5 Result: Level B1 Result: Level B1 Level B2 Result: Level B1 Level B2 Level B3 Result: Level B1 Level B2 Level B3 Level B4 Result: Level B1 Level B2 Level B3 Level B4 Level B5
Meaning that once it hit level 5, it will wipe out the old values and print the new level until it hit level 5 again and wipe out the old values again.

Thanks

Replies are listed 'Best First'.
Re: Wiping Out Values
by Chady (Priest) on May 26, 2001 at 10:31 UTC

    just something from the top of my head, you might try:

    for $level (@achievement) { push @result, $level; print 'Result: ', join (' ', @result), "\n"; @result = () if ($level =~ /5$/); }

    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
Re: Wiping Out Values
by the_slycer (Chaplain) on May 26, 2001 at 10:32 UTC
    What you want is to start over at level X1. So, the following is one way to do it.
    foreach (@achievement){ @results = () if /level [BC]1/; push (@results,$_); print "Result = @results\n"; }
      Or, alternatively, you could merely reset when the number goes down instead of going up. As in:
      my $l_level = 0; foreach (@achievement){ my ($level) = /(\d+)$/; @results = () if ($level < $l_level); push (@results,$_); print "Result = @results\n"; $l_level = $level; }