z*y 发帖数: 1311 | 1 I want to declare an array of file pointers. For example in C, it will look
like: FILE *fp[n].
However, I don't know how to do it in perl. Here is what I tried but failed.
my @in = ();
$n = $#ARGV + 1;
for ($i=0; $i < $n; $i++)
{
open($in[$i], "<" . $ARGV[$i]);
}
Thank you for your help. | r****t 发帖数: 10904 | 2 perl不知道,python一行就行:
fps = [open(fn) for fn in sys.argv[1:]]
look
failed.
【在 z*y 的大作中提到】 : I want to declare an array of file pointers. For example in C, it will look : like: FILE *fp[n]. : However, I don't know how to do it in perl. Here is what I tried but failed. : my @in = (); : $n = $#ARGV + 1; : for ($i=0; $i < $n; $i++) : { : open($in[$i], "<" . $ARGV[$i]); : } : Thank you for your help.
| t****t 发帖数: 6806 | 3 how does it fail? this piece of code runs well on my system.
one thing you may want to know, if you put (the reference of) a filehandle
in an element of array, you may not use <...> to read a line from it. in
other word, or <$FH> works, but anything other than bareword or simple
scalar variable is interpreted as fileglob. if you have to use an array of (
the references of) filehandles, use readline(...) instead of <...>.
look
failed.
【在 z*y 的大作中提到】 : I want to declare an array of file pointers. For example in C, it will look : like: FILE *fp[n]. : However, I don't know how to do it in perl. Here is what I tried but failed. : my @in = (); : $n = $#ARGV + 1; : for ($i=0; $i < $n; $i++) : { : open($in[$i], "<" . $ARGV[$i]); : } : Thank you for your help.
|
|