English 中文(简体)
我怎么能写一份从档案中读出的《每》文本? [复制]
原标题:How can I write a Perl Script that reads from a file? [duplicate]
  • 时间:2011-11-23 03:58:43
  •  标签:
  • perl

我想写一个读物的“Perl”文本。 编号一栏,

20  30  12
31  20  54
63  30  21
11  12  10

and do some calculations, for example the mean. I don t know how to declare and initialize it. I ve got this example, in which is looking for the median, and it has the data declared, in my case, the data is in a file, not in the script and want to calculate the median. there is it.. #!/usr/bin/perl

#data points 
@vals = ( 33, 23, 55, 39, 41, 46, 38, 52, 34, 29, 27, 51, 33, 28 ); 
print "UNSORTED: @vals
"; 

#sort data points 
@vals = sort(@vals); 
print "SORTED: @vals
"; #test to see if there are an even number of data points 
if( @vals % 2 == 0) { 

#if even then: 
$sum = $vals[(@vals/2)-1] + $vals[(@vals/2)]; 
$med = $sum/2; 
print "The median value is $med
";
}
else{ 
#if odd then: 
print "The median value is $vals[@vals/2]
";
} 
exit;
最佳回答

这艘护卫舰乘以第二只:

perl -lane  print $F[0] * $F[1]  <FILE>

http://www.un.org。 • 附有新要求的每字母版和有3栏的档案:

#!/usr/bin/perl

use strict;
use warnings;

my (@vals, $sum, $med);

while (<>) {
    @vals = split;

    print "UNSORTED: @vals
"; 

    #sort data points 
    @vals = sort(@vals); 
    print "SORTED: @vals
"; #test to see if there are an even number of data points 

    if(@vals % 2 == 0) { 
        #if even then: 
        $sum = $vals[(@vals/2)-1] + $vals[(@vals/2)]; 
        $med = $sum/2; 
        print "The median value is $med
";
    }
    else{ 
        #if odd then: 
        print "The median value is $vals[@vals/2]
";
    } 

    print "
";
}

You may understand what s going on instead of just cut & paste ;)

操作文字:

./script.pl file_with_cols.txt
问题回答

Use the open command. Plenty of good examples on that page.

此处建议使用的职能(见功能参考资料):

  1. Open your file for reading: use the open function
  2. Loop through each line: while (my $line = <$filehandle>)
  3. Remove the trailing newline: use chomp
  4. Extract the values from each line: use the split function
  5. Store the values in an array: use push

To verify that your array has what you want at the end:

use Data::Dumper;
print Dumper @vals;

UPDATE

如果不给你整个答案(因为这是家事),就在职能参考的每一条目中都看着样本代码。

这里,大家开始:

open my $filehandle,  < , $filename
    or die "Couldn t open $filename";
while (my $line = <$filehandle>) {
    # do stuff with $line
}
close $filehandle;




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签