V
Vinod. K
Hi everybody,
I have a small query with foreach loop( as well as while loop).
Here is the small script with which i got a problem
----------------------------------------------------
#!/usr/local/perl/5.6.0/bin/perl
my @a=qw/ref txt dat/;
chomp @a;
foreach(@a)
{
my $fl=$_;
if(<*$fl>)
{
chomp $a;
$a=<*$fl>;
print $a,"\n";
}
else
{
print"no files\n";
}
}
-----------------------------------------
O/P of this program(test.pl) :
prompt>test.pl
a.ref
no files
prompt>
--------------------------------------------
Here in the above program, I am trying to list out the files which are
ending with ".ref" or ".txt" or ".dat" extension. But the output I am
getting here is only a file with .ref extension which is first element
of array,@a, even though each one of above said extension files are
existing under a dir from where I ran the this program.
Here .ref is the first element of array,@a. I observed that this
program lists only the files of extension which is there in the first
element of the array,@a and lists only to the maximum of 3 files
eventhough more than 3 files of the same extension are present under
the dirctory. if we change the content of first element to some other
extension, then it will lists files of that extension.
I don't know why it is so??? I am not able understand the logic behind
this foreach loop( and even with while loop too). why it is not
listing files of all the extensions(i.e .dat, .txt, and .ref)???
But if we use unix command, "ls" instead of diamond operator(<>),then
the same above program works fine.
is the probelm with diamond operator or loops???
Please help me out in clearing all my doubts.
Thanks for invaluable time,
- vinod.
I have a small query with foreach loop( as well as while loop).
Here is the small script with which i got a problem
----------------------------------------------------
#!/usr/local/perl/5.6.0/bin/perl
my @a=qw/ref txt dat/;
chomp @a;
foreach(@a)
{
my $fl=$_;
if(<*$fl>)
{
chomp $a;
$a=<*$fl>;
print $a,"\n";
}
else
{
print"no files\n";
}
}
-----------------------------------------
O/P of this program(test.pl) :
prompt>test.pl
a.ref
no files
prompt>
--------------------------------------------
Here in the above program, I am trying to list out the files which are
ending with ".ref" or ".txt" or ".dat" extension. But the output I am
getting here is only a file with .ref extension which is first element
of array,@a, even though each one of above said extension files are
existing under a dir from where I ran the this program.
Here .ref is the first element of array,@a. I observed that this
program lists only the files of extension which is there in the first
element of the array,@a and lists only to the maximum of 3 files
eventhough more than 3 files of the same extension are present under
the dirctory. if we change the content of first element to some other
extension, then it will lists files of that extension.
I don't know why it is so??? I am not able understand the logic behind
this foreach loop( and even with while loop too). why it is not
listing files of all the extensions(i.e .dat, .txt, and .ref)???
But if we use unix command, "ls" instead of diamond operator(<>),then
the same above program works fine.
is the probelm with diamond operator or loops???
Please help me out in clearing all my doubts.
Thanks for invaluable time,
- vinod.