Hi Anne<br><br>2 reasons why your while loop does not terminate on empty input:<br><br>First, the 'length' issue: Before chomping, length of regexpr when you
just press Enter is 1 (Still has the newline character) , after
chomping it is 0<br>
So you need to check after chomping or say <br><br> ....<br> while (length($regexpr = <STDIN>)>1) { # 1<br> ....<br>
<br>Secondly, after chomping, the empty string is still a defined value:<br><br>perl -e 'print "defined" if defined "" '<br><br>The reason why presumably the defined test is part of the loop, is that it if you enter<br>
Ctrl-D (That is on *nix) and dont check for it, and you use warnings (which you should be doing), you will get a warning <br>
<br>Ctrl-C is a *nix shell control character which terminates the command, so does not get to be processed in the while loop.<br>
<br>The reason all the files are printed out with an empty string is that it matches all the filenames<br><br>perl -e 'print "match" if "anyfilenameyoulike"=~//'<br>perl -e 'print "match" if ""=~//'<br>
<br>Regards<br><br><div class="gmail_quote">On Sun, Aug 9, 2009 at 2:39 PM, Anne Wainwright <span dir="ltr"><<a href="mailto:anotheranne@fables.co.za">anotheranne@fables.co.za</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi, all,<br>
<br>
(Am in fact referring to Exercise 2 Chapter 2 of<br>
Schwartz et al INTERMEDIATE PERL.)<br>
<br>
First, while realising that the official solution is a masterpiece of<br>
condensed perl code, I don't understand why the first<br>
line has the (1) ).<br>
<br>
-----------------------------------------------------------<br>
while(1) {<br>
print "Enter a regular expression to match filenames> ";<br>
chomp(my $regex = <STDIN>);<br>
last unless (defined $regex && length $regex);<br>
print map {" $_\n"} grep {eval{/$regex/}} glob(".* *");<br>
}<br>
-----------------------------------------------------------<br>
<br>
My own more primitive solution fails to get out of the<br>
input loop, albeit with simpler criteria. My code is:<br>
<br>
-------------------------------------------------------<br>
opendir THISDIR, "." or die "serious braindamage: $!";<br>
@allfiles = readdir THISDIR;<br>
closedir THISDIR;<br>
<br>
while (defined($regexpr = <STDIN>)) { # 1<br>
chomp $regexpr;<br>
foreach $file(@allfiles) { # 2<br>
if ($file =~ m/$regexpr/) { # 3<br>
print "$file\n";<br>
} # -3<br>
} # -2<br>
} # -1<br>
-------------------------------------------------------<br>
Essentially on no regex input it outputs all the files as if ".+" had<br>
been input.<br>
<br>
I thought the line marked #1 was a dead ringer for getting out of the<br>
input loop. (p.72 Chapter 6 of Schwartz et al LEARNING PERL). If I take<br>
"&& length $regex" out of their code then that also fails to exit. I<br>
don't understand :(<br>
<br>
Any one explaining this in small understandable words thanked<br>
in advance.<br>
<br>
bestest<br>
Anne<br>
_______________________________________________<br>
Za-pm mailing list<br>
<a href="mailto:Za-pm@pm.org">Za-pm@pm.org</a><br>
<a href="http://mail.pm.org/mailman/listinfo/za-pm" target="_blank">http://mail.pm.org/mailman/listinfo/za-pm</a><br>
</blockquote></div><br><br clear="all"><br>