假设文件1有两列,看起来像:
fuzz n. flowering shrub of the rhododendron family dyspeptic adj. bright blue, as of the sky dysplexi adj. of Byzantium or the E Roman Empire eyrie adj. of the Czech Republic or Bohemia azalea adj. suffering from dyslexia Czech adj. suffering from dyspepsia Byzantine n. eagle's nest azure n. mass of soft light particle
文件2只有一个clumn,看起来像:
azalea azure Byzantine Czech dyslexic dyspeptic eyrie fuzz
我希望文件1的第一列替换为文件2的列.因此,文件3应如下所示:
azalea n. flowering shrub of the rhododendron family azure adj. bright blue, as of the sky Byzantine adj. of Byzantium or the E Roman Empire Czech adj. of the Czech Republic or Bohemia dyslexic adj. suffering from dyslexia dyspeptic adj. suffering from dyspepsia eyrie n. eagle's nest fuzz n. mass of soft light particle
我有一种感觉,就是有一种或另一种简单的方法可以做这种工作而且它很可能是一些方便的模块,但是现在我甚至不能以最低效的方式做到这一点.我尝试了一堆代码
while<$line1 = file1>{
while<$line2 = file2>{
join $line,$line2
Run Code Online (Sandbox Code Playgroud)
但没有运气.有人能指出我正确的方向吗?一如既往地感谢任何指导.
如果你想同时读两行,试试这个:
while(defined(my $line1 = <file1>)
and defined(my $line2 = <file2>)) {
# replace contents in $line1 with $line2 and do something with $line1
}
Run Code Online (Sandbox Code Playgroud)
一旦一行耗尽,这将停止工作,因此在此循环结束时查看两个文件是否为空可能是个好主意:
die "Files are different sizes!\n" unless eof(file1) == eof(file2);
Run Code Online (Sandbox Code Playgroud)
当然,在现代Perl中,您可以将文件句柄存储在词法范围的变量中,如下所示:
open my $fh, ...
Run Code Online (Sandbox Code Playgroud)
然后<FILEHANDLES>用漂亮的lexically scoped 替换丑陋的全局<$filehandles>.它更好,而且它更好
| 归档时间: |
|
| 查看次数: |
1116 次 |
| 最近记录: |