如何同时计算两次出现的频率?

时间:2016-03-22 17:49:12

标签: perl

所以我有一个如下所示的数据集:

try {
    $p = new PDFlib();
    if ($p->begin_document("wp-content/uploads/hello.pdf", "") == 0) {
            die("Error: " . $p->get_errmsg());
    }
    $p->set_info("Creator","hello.php"); 
    $p->set_info("Author","Rasmus Lerdorf"); 
    $p->set_info("Title","Hello world (PHP)");
    $p->set_parameter("FontOutline", "Ubuntu=/var/www/html/wordpress/wp-includes/fonts/ubuntu-font-family/Ubuntu-C.ttf"); 
    $p->set_parameter("textformat", "utf8"); 
    $y = 2138;
    $x = 1512;
    $p->begin_page_ext($x, $y, "");
    $p->set_text_pos(25,750);
    $font = $p->findfont("Ubuntu", "unicode", "");
    $font_size=24.0;  //font size, used to space lines on y axis
    $p->setfont($font, $font_size);
    $p->continue_text("(".chr(128)." Ç à á â ã ç è é ê)");
    $p->end_page_ext("");
    $p->end_document(""); 
}catch (\PDFlibException $e) {
    die("PDFlib exception occurred in hello sample:\n" .
    "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
    $e->get_errmsg() . "\n");
}catch (\Exception $e) {
    die($e);
}

我正在尝试让perl打印每行中前两个值的每个出现的最后一个值。因此,我的目标是打印以下内容:

2,1,10,2,0
2,1,23,2,1
2,3,6,2,0
5,1,4,3,0
5,2,2,2,0
5,2,2,3,1
5,2,8,1,2
5,3,6,3,1
8,2,12,1,0

我如何告诉perl为每个出现的数字配对打印行的最后一个值?

2 个答案:

答案 0 :(得分:0)

不确定您的频率是什么意思,但此代码

#!perl
use strict;
use warnings;

generate_output(2, 1);
generate_output(2, 3);
generate_output(5, 1);
generate_output(5, 2);
generate_output(5, 3);
generate_output(8, 2);

sub generate_output
{
    my ($a, $b) = @_;

    print "$a $b:";

    my $data_start = tell DATA;

    while (<DATA>) {
        chomp;
        my @columns = split /,/; 
        if ($columns[0] == $a and $columns[1] == $b)
        {
            print " ", $columns[-1];
        }
    }
    print "\n";
    seek DATA, $data_start, 0;
}

__DATA__
2,1,10,2,0
2,1,23,2,1
2,3,6,2,0
5,1,4,3,0
5,2,2,2,0
5,2,2,3,1
5,2,8,1,2
5,3,6,3,1
8,2,12,1,0

生成您想要的输出:

2 1: 0 1
2 3: 0
5 1: 0
5 2: 0 1 2
5 3: 1
8 2: 0

答案 1 :(得分:0)

use strict;
#use Data::Dumper;
my $h;

while (<>){
    chomp;
    my @linechars = split /,/;
    #Use the first two characters of the input line as the key to a hash.
    my $key = $linechars[0]." ".$linechars[1];
    #stick the last character into an array for this key
    push @{$h->{$key}}, $linechars[4];
}

#print Dumper($h);

foreach my $key (keys%{$h}){
    print "$key:";
    foreach (@{$h->{$key}})
    {
        print " $_";
    }
    print "\n";
}

给出

-bash-3.2$ perl tmp.pl tmp.txt
5 1: 0
2 3: 0
8 2: 0
2 1: 0 1
5 2: 0 1 2
5 3: 1

如果您只想要频率,请替换

    foreach (@{$h->{$key}})
    {
        print " $_";
    }

    print scalar (@{$h->{$key}})."\n"