On Sep 16, Wijaya Edward said:

But how come my code below doesn't give
the intended result as below? How can I go about it?

Simply put, because the 'foreach my $line (keys %line)' creates the list of keys ahead of time. Here's one way to achieve your goal:

  my @stack = qw( A B C );
  my @tojoin = qw( W X Y Z );
  my @result;

  # while there is still something in @stack
  while (@stack) {
    # take the first element off, put it in $next
    # and add it to our @result array
    my $next = shift @stack;
    push @result, $next;

    # STOP processing @stack if this element is 'AYW'
    last if $next eq 'AYW';

    # for each suffix in @tojoin...
    for (@tojoin) {
      # add an element to @stack that is the
      # CURRENT element with the suffix added
      push @stack, "$next$_";
    }
  }

You can, of course, use a hash for @result instead of the array like I did.

--
Jeff "japhy" Pinyan        %  How can we ever be the sold short or
RPI Acacia Brother #734    %  the cheated, we who for every service
http://www.perlmonks.org/  %  have long ago been overpaid?
http://princeton.pm.org/   %    -- Meister Eckhart

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to