Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > having trouble with control structures

Reply
Thread Tools

having trouble with control structures

 
 
M. Duijkers
Guest
Posts: n/a
 
      12-10-2004
Heya... currently working my way through the book "learning perl". Just
having trouble with control structures.I want to do smth simple but can only
come with a bulky and ugly answer. What I want my program to do is "recieve
filenames from stdin ina loop, and when you recieve the string "QUIT" then
quit.
This is how I did it, but I just KNOW there is a better and easier way for
such a common task. Any sugs really appriciated
Mike.

#!perl -w
do
{
print "Enter filename or QUIT to stop: ";
$filename = <STDIN>;
chomp $filename;
if ($filename ne "QUIT")
{
if (! -e $filename)
{
print "file does not exist\n";
last;
}
if (-r $filename)
{
print "file is readable \n";
}
else
{
print "file is NOT readable \n";
}
if (-w $filename)
{
print "file is writeable\n";
}
else
{
print "file is NOT writeable \n";
}
if (-x $filename)
{
print "file is executable\n";
}
else
{
print "file is NOT excecuteable \n";
}
}
else
{
exit 0;
}
}while ($filename ne "QUIT")



 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      12-10-2004
"M. Duijkers" <(E-Mail Removed)> wrote in message
news:41ba020e$0$19389$(E-Mail Removed)4all.nl...
> Heya... currently working my way through the book "learning perl".

Just
> having trouble with control structures.I want to do smth simple but

can only

What is "smth"? Please do not use slang. Not everyone who reads this
group is a native English speaker.

> come with a bulky and ugly answer. What I want my program to do is

"recieve
> filenames from stdin ina loop, and when you recieve the string "QUIT"

then
> quit.
> This is how I did it, but I just KNOW there is a better and easier way

for
> such a common task. Any sugs really appriciated


same with "sugs".

> Mike.
>
> #!perl -w
> do
> {
> print "Enter filename or QUIT to stop: ";
> $filename = <STDIN>;
> chomp $filename;
> if ($filename ne "QUIT")
> {


<snip a bunch of irrelevant file tests>
-- Please post a *short* but complete program that demonstrates your
problem, eliminating all other factors.

> }
> else
> {
> exit 0;
> }
> }while ($filename ne "QUIT")
>


So basically you want to know how to make this do-while loop without the
extra test for 'QUIT'?

while (chomp (my $filename=<STDIN>)){
last if $filename eq 'QUIT'){
#file tests...
}

Paul Lalli

 
Reply With Quote
 
 
 
 
Tore Aursand
Guest
Posts: n/a
 
      12-15-2004
On Fri, 10 Dec 2004 21:07:45 +0100, M. Duijkers wrote:
> #!perl -w
> do
> {
> print "Enter filename or QUIT to stop: ";
> $filename = <STDIN>;
> chomp $filename;
> if ($filename ne "QUIT")
> {
> if (! -e $filename)
> {
> print "file does not exist\n";
> last;
> }
> if (-r $filename)
> {
> print "file is readable \n";
> }
> else
> {
> print "file is NOT readable \n";
> }
> if (-w $filename)
> {
> print "file is writeable\n";
> }
> else
> {
> print "file is NOT writeable \n";
> }
> if (-x $filename)
> {
> print "file is executable\n";
> }
> else
> {
> print "file is NOT excecuteable \n";
> }
> }
> else
> {
> exit 0;
> }
> }while ($filename ne "QUIT")


Here is my suggestion for a solution;

#!/usr/bin/perl
#
use strict;
use warnings;

while ( <DATA> ) {
chomp;
last if ( $_ eq 'QUIT' );

if ( -e ) {
my %status = ('readable' => -r,
'writeable' => -w,
'executable' => -x);

my @status = map { ( $status{$_} ) ? $_ : 'NOT ' . $_ } keys %status;
print "'$_' is " . join( ', ', @status ) . "\n";
}
else {
print "'$_' doesn't exist!\n";
}
}


--
Tore Aursand <(E-Mail Removed)>
"First, God created idiots. That was just for practice. Then He created
school boards." (Mark Twain)
 
Reply With Quote
 
Tore Aursand
Guest
Posts: n/a
 
      12-15-2004
On Wed, 15 Dec 2004 16:02:10 +0100, Tore Aursand wrote:
> while ( <DATA> ) {


Should have been <STDIN> instead of <DATA>, of course; I tested it with a
list of filenames in the __DATA__ section of my Perl script.


--
Tore Aursand <(E-Mail Removed)>
"Writing is a lot like sex. At first you do it because you like it.
Then you find yourself doing it for a few close friends and people you
like. But if you're any good at all, you end up doing it for money."
(Unknown)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
structures, structures and more structures (questions about nestedstructures) Alfonso Morra C Programming 11 09-24-2005 07:42 PM
Having trouble with a RadioButtonList control Mark Broadbent ASP .Net 3 12-15-2004 06:11 PM
newbie having trouble with control structures: loop till recieve string QUIT M. Duijkers Perl 2 12-12-2004 11:02 AM
Having trouble adding a web user control programatically. Shawn ASP .Net Building Controls 4 08-01-2004 12:53 PM
Type Casting IPv4 and IPv6 structures to Generic Structures tweak C Programming 14 06-11-2004 02:43 PM



Advertisments