while loop display of files in a directory

Discussion in 'Perl Misc' started by mike, Mar 2, 2004.

  1. mike

    mike Guest

    hi

    i did a code to show the files in a directory
    say

    while(<test*.txt>)
    {
    print ;

    }

    it will show all the files with beginning with test and ending with .txt
    but when i assigned it to a variable like this

    $files = "test\*\.txt";
    while(<"$files">)
    {
    print ;
    }
    it doesn't show the files.
    Why is this so?
    thanks
     
    mike, Mar 2, 2004
    #1
    1. Advertising

  2. mike

    ko Guest

    mike wrote:
    > hi
    >
    > i did a code to show the files in a directory
    > say
    >
    > while(<test*.txt>)
    > {
    > print ;
    >
    > }


    This is does a filename glob.

    > it will show all the files with beginning with test and ending with .txt
    > but when i assigned it to a variable like this
    >
    > $files = "test\*\.txt";
    > while(<"$files">)
    > {
    > print ;
    > }
    > it doesn't show the files.
    > Why is this so?
    > thanks


    In simple terms, this tries to read from a filehandle. Its documented in
    perlop (do 'perldoc perlop' from your shell), the section titled 'I/O
    Operators'.

    Something like this is better suited to do what you want:

    my $files = 'test*.txt';
    print $_, "\n" while ( glob $files ); # or (glob 'test*.txt')

    More reading:

    perldoc -f glob
    perldoc -f readline

    HTH -keith
     
    ko, Mar 2, 2004
    #2
    1. Advertising

  3. mike wrote:
    >
    > hi
    >
    > i did a code to show the files in a directory
    > say
    >
    > while(<test*.txt>)
    > {
    > print ;
    >
    > }
    >
    > it will show all the files with beginning with test and ending with .txt
    > but when i assigned it to a variable like this
    >
    > $files = "test\*\.txt";
    > while(<"$files">)
    > {
    > print ;
    > }
    > it doesn't show the files.
    > Why is this so?


    You added quotation marks to the file glob but you probably don't have
    any files that start with "test and end with .txt".


    John
    --
    use Perl;
    program
    fulfillment
     
    John W. Krahn, Mar 2, 2004
    #3
    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. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,239
    Tim Slattery
    Mar 30, 2005
  2. Daniel Pitts
    Replies:
    14
    Views:
    1,239
    Patricia Shanahan
    Dec 23, 2006
  3. Uday Bidkar
    Replies:
    4
    Views:
    494
    =?ISO-8859-15?Q?Juli=E1n?= Albo
    Dec 12, 2006
  4. Roedy Green
    Replies:
    3
    Views:
    436
    Mike Schilling
    Sep 13, 2008
  5. Isaac Won
    Replies:
    9
    Views:
    384
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page