while loop display of files in a directory

M

mike

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
 
K

ko

mike said:
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
 
J

John W. Krahn

mike said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top