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

I'm trying to learn perl, and I'm going through the tutorials and such, and I'm trying to figure out how to end this loop so that when it finishes, it doesn't go back up and restart. Anybody have any ideas? Here is the code:
while (<>) { if(/dick/){ print "you said dick!\n"; } else { print "you didn't say dick, hah, what a dick!\n"; } }
Thanks,
jrbush82

Replies are listed 'Best First'.
Re: ending this loop
by suaveant (Parson) on Aug 10, 2001 at 20:50 UTC
    The last function will finish the current loop... and perlfunc:next|next] will immediately take you to the top and start the next iteration of the loop

                    - Ant
                    - Some of my best work - Fish Dinner

      Thanks, I got it working with this:
      while(<>){ if(/dick/){ print "you said dick!\n"; } else { print "you didn't say dick, hah, what a dick!\n"; } last; }
      Thanks again,
      jrbush82
        And if you did the following...
        while(<>){ if(/dick/){ print "you said dick!\n"; last; } else { print "you didn't say dick, hah, what a dick!\n"; } }
        it would keep waiting for input until it got 'dick'

                        - Ant
                        - Some of my best work - Fish Dinner

        That will always quit after the first pass. So why do you have a loop at all? If that "works", what you really want is just:
        $_= <>; if(/dick/){ print "you said dick!\n"; } else { print "you didn't say dick, hah, what a dick!\n"; }
Re: ending this loop
by arturo (Vicar) on Aug 10, 2001 at 20:56 UTC

    I'm not sure what you're asking here. Is the idea that if you find the string you're looking for, print the message and end? If that's the case, then what you want is last :

    my $found=0; while(<>) { if (/woot!/) { print "You said woot!\n"; $found=1; last; } } print "Nobody said 'woot!'\n" unless $found;

    Notice also the addition of the variable that's set to a true value if you find the string you're looking for.

    HTH (Oh, and BTW, since this is a public site ... you might want to pick less 'ripe' examples =)

    perl -e 'print "How sweet does a rose smell? "; chomp ($n = <STDIN>); +$rose = "smells sweet to degree $n"; *other_name = *rose; print "$oth +er_name\n"'
Re: ending this loop
by mexnix (Pilgrim) on Aug 10, 2001 at 20:52 UTC
    I'm not sure if this answers your question, but I think this is what you want...

    for (@ARGV) { if(/dick/){ print "you said dick!\n"; } else { print "you didn't say dick, hah, what a dick!\n"; } }

    OR

    /dick/ ? print "you said dick!\n" : print "you didn't say dick, hah, +what a dick!\n" for (@ARGV);

    __________________________________________________
    <moviequote name="Hackers">
    The Plague: [...]Well let me explain the New World Order. Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo!
    </moviequote>

    mexnix.perlmonk.org