亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 3538 | 回復(fù): 7
打印 上一主題 下一主題

求教 如何從文件中每次讀取固定數(shù)目的行 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2008-07-16 17:42 |只看該作者 |倒序?yàn)g覽
20可用積分
比如一個(gè)文本文件,我想做個(gè)循環(huán),每次讀入10行進(jìn)行操作,請(qǐng)問有什么簡(jiǎn)單些的方法實(shí)現(xiàn)沒有?

多謝了先

最佳答案

查看完整內(nèi)容

package MyFileIO;use base 'IO::File';sub get_n_lines { my $self = shift; my $lines = shift || 1; #default for 1 line. if (wantarray) { my @t; for (1 .. $lines) { push @t, $self->getline; } return @t; } my $t; for (1 .. $lines) { $t .= $self->getline; } return $t;}1;------#! /usr/bin/env perluse strict;use warnings;use MyFileIO;my $file = MyFileIO->new('test.pl') or die;my $test = $file->ge ...

論壇徽章:
0
2 [報(bào)告]
發(fā)表于 2008-07-16 17:42 |只看該作者
package MyFileIO;

use base 'IO::File';

sub get_n_lines {
        my $self = shift;
        my $lines = shift || 1;  #default for 1 line.

        if (wantarray) {
                my @t;
                for (1 .. $lines) {
                        push @t, $self->getline;
                }

                return @t;
        }

        my $t;
        for (1 .. $lines) {
                $t .= $self->getline;
        }

        return $t;
}

1;


------

#! /usr/bin/env perl

use strict;
use warnings;

use MyFileIO;

my $file = MyFileIO->new('test.pl') or die;

my $test = $file->get_n_lines(10);

print $file->get_n_lines(10);

print $test;
___________________

給你寫一個(gè)吧,現(xiàn)在掙個(gè)工分可真不容易啊

論壇徽章:
0
3 [報(bào)告]
發(fā)表于 2008-07-16 19:57 |只看該作者
sub read_n_lines {
        # read_n_times...
}



[ 本帖最后由 cobrawgl 于 2008-7-16 20:24 編輯 ]

論壇徽章:
0
4 [報(bào)告]
發(fā)表于 2008-07-16 23:02 |只看該作者
if (wantarray) {
這個(gè),學(xué)到了。

論壇徽章:
0
5 [報(bào)告]
發(fā)表于 2008-07-17 11:20 |只看該作者

回復(fù) #3 cobrawgl 的帖子

你都有那么多分了。俺還得多逛水

論壇徽章:
0
6 [報(bào)告]
發(fā)表于 2008-07-17 17:26 |只看該作者

回復(fù) #3 cobrawgl 的帖子

學(xué)會(huì)了.

論壇徽章:
0
7 [報(bào)告]
發(fā)表于 2008-07-23 16:29 |只看該作者
昨天學(xué)了一招
可以試試這段代碼:

$lineNum = 1;
while ( $lineNum <= 1000 ) {
        &readTenLines($lineNum);
        print "------------------------------------------------------\n";
        $lineNum += 10;
}
       
sub readTenLines() {
        open FILE, "< file.txt";
        my ($begin) = @_;
        my $end = $begin + 10;
        while (<FILE>) {
                print if ( $. >= $begin && $. <= $end );
        }
        close FILE;
}

論壇徽章:
0
8 [報(bào)告]
發(fā)表于 2008-07-28 09:52 |只看該作者

多謝各位

在此謝過~
您需要登錄后才可以回帖 登錄 | 注冊(cè)

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報(bào)專區(qū)
中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP