回 帖 发 新 帖 刷新版面

主题:问一个文件输出的问题?

我编了一个打开文件,然后输出文件内容的小程序,结果输出总是不对,哪位能帮忙看一下? “myfile.dat”文件和程序在同一个目录下面,而且里面有内容,输出的结果总是为空。


#! /usr/bin/perl

$filename = "myfile.dat";
open (FILE, "$filename") || print "Can not open myfile.dat!\n";
@myflie = <FILE>;
close FILE;

print " myfile is: ", @myfile;

foreach $line (@myfile)
{
    print $line;
}

# now print the number of lines in @myfile
print "\n lines: ", $#myfile+(1-$[);

# now print the number of chars in @myfile
foreach $line (@myfile)
{
    $chars += length($line);
}
print "\n characters: ", $chars; 

------------------------------------
[color=FF0000]输出结果如下:[/color]

 myfile is:
 lines: 0
 characters:

回复列表 (共1个回复)

沙发

你把 "@myfile" 给拼错了.
#! /usr/bin/perl

$filename = "myfile.dat";
open (FILE, "$filename") || print "Can not open myfile.dat!\n";
///////////////
@myfile = <FILE>;   <-你写了"@myflie"
////////////////
close FILE;

print " myfile is: ", @myfile;

foreach $line (@myfile)
{
    print $line;
}

# now print the number of lines in @myfile
print "\n lines: ", $#myfile+(1-$[);

# now print the number of chars in @myfile
foreach $line (@myfile)
{
    $chars += length($line);
}
print "\n characters: ", $chars;
////////////////////////
输出结果如下:
 myfile is: #! /usr/bin/perl -w
 while (<>) {
        while ( /(\b[^\W_\d][\w'-]+\b)/g ) {   # misses "`sheep'"
            $seen{$1}++;
        }
    }
    while ( ($word, $count) = each %seen ) {
        print "$count $word\n";
    }#! /usr/bin/perl -w
 while (<>) {
        while ( /(\b[^\W_\d][\w'-]+\b)/g ) {   # misses "`sheep'"
            $seen{$1}++;
        }
    }
    while ( ($word, $count) = each %seen ) {
        print "$count $word\n";
    }
 lines: 9
 characters: 195
我在这打开的文件是另外一个PERL程序

我来回复

您尚未登录,请登录后再回复。点此登录或注册