Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > while loop display of files in a directory

Reply
Thread Tools

while loop display of files in a directory

 
 
mike
Guest
Posts: n/a
 
      03-02-2004
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
 
Reply With Quote
 
 
 
 
ko
Guest
Posts: n/a
 
      03-02-2004
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
 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      03-02-2004
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
 
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
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Re: How to loop through a list while inside the loop, the list size may be decreased? Roedy Green Java 3 09-13-2008 01:51 AM
Opinion poll: for loop vs while loop with Iterators. Daniel Pitts Java 14 12-23-2006 05:22 AM
Whats the difference between while loop in Windows message loop and while(1) Uday Bidkar C++ 4 12-12-2006 12:30 PM
while loop in a while loop Steven Java 5 03-30-2005 09:19 PM



Advertisments