having trouble with control structures

Discussion in 'Perl Misc' started by M. Duijkers, Dec 10, 2004.

  1. M. Duijkers

    M. Duijkers Guest

    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")
    M. Duijkers, Dec 10, 2004
    #1
    1. Advertising

  2. M. Duijkers

    Paul Lalli Guest

    "M. Duijkers" <> wrote in message
    news:41ba020e$0$19389$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
    Paul Lalli, Dec 10, 2004
    #2
    1. Advertising

  3. M. Duijkers

    Tore Aursand Guest

    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 <>
    "First, God created idiots. That was just for practice. Then He created
    school boards." (Mark Twain)
    Tore Aursand, Dec 15, 2004
    #3
  4. M. Duijkers

    Tore Aursand Guest

    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 <>
    "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)
    Tore Aursand, Dec 15, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. M. Duijkers
    Replies:
    2
    Views:
    476
    Joe Smith
    Dec 12, 2004
  2. Mark Broadbent

    Having trouble with a RadioButtonList control

    Mark Broadbent, Dec 15, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    1,929
    Saravana
    Dec 15, 2004
  3. tweak
    Replies:
    14
    Views:
    2,779
    Eric Sosman
    Jun 11, 2004
  4. Alfonso Morra
    Replies:
    11
    Views:
    713
    Emmanuel Delahaye
    Sep 24, 2005
  5. Shawn

    Having trouble adding a web user control programatically.

    Shawn, Jul 31, 2004, in forum: ASP .Net Building Controls
    Replies:
    4
    Views:
    144
    John Saunders
    Aug 1, 2004
Loading...

Share This Page