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

Hi I have to very basic question. I have a loop with foreach() however I don't want to wait it finish if I find my data before the end. How to break the foreach() loop? There is a instruction to do it? The second question is how to tell to print to a specific position? For example, I always want to write starting at position 30. Thank you
  • Comment on Break foreach and print at specific position?

Replies are listed 'Best First'.
Re: Break foreach and print at specific position?
by ikegami (Patriarch) on May 10, 2010 at 22:58 UTC

    last

    print(' ' x 29, ...);

Re: Break foreach and print at specific position?
by toolic (Bishop) on May 10, 2010 at 23:02 UTC
    How to break the foreach() loop? There is a instruction to do it?
    last

    See also Loop Control

    The second question is how to tell to print to a specific position? For example, I always want to write starting at position 30.

    Update: due to subsequent clarification of the question, this does not solve the problem:

    If you only want to print a portion of a string, use substr:

    print substr($s, 29);
      LAST is exactly what I was looking for, thanks. I think I was not clear on my print question. I want for example, always start printing at position 30. For example, If I do print "\nValue is $data"; it will print the value on the start of the line and it's correct since I used \n, however I want to write on the new line bug starting at the position 30, in other works I would like to print always jumping the first 29 position / chars. How to do it? Thank you
        The simplest way is simply to add 28 spaces in front:
        my $indent = " " x 28; say $indent,"your message";
        I used a variable to save having to change x 28 however many places around the program when the indentation gets changed.
        i can't remember off the top of my head the exact perl variable, but anyhow you can just change the end of line character to any character or any number of characters you want. after it is set, print a blank line, and all your prints now default at whatever position. If you need more control over print layout, you could use the (a bit arcane) FORMAT constructs, or use some other layout module.
        the hardest line to type correctly is: stty erase ^H