Hi Everyone,
a.txt
line1;a;33
line1;c;22
line1;b;0
line1;a;55
a.pl
#!/usr/bin/perl
use strict;
use warnings;
my @sorted=();
my @tmp;
my $FA;
my @F;
my %q;
my %qq;
open($FA, "/tmp/a.txt") or die "$!";
while(<$FA>) {
chomp;
@tmp=split(/\;/, $_);
push @sorted, "$_";
}
close $FA;
foreach (@sorted) {
@F = split(/\;/, $_);
$q{"$F[1]"} += 1;
if ($F[2] > 0) {
$qq{"$F[1]"} += 1;
} else {
$qq{"$F[1]"} += 0;
}
}
print map "$_\t$q{$_}\t$qq{$_}\t".sprintf ("%.2f",60/$qq{$_})."\n", sort { $q{$b} <=> $q{$a} } keys %q;
[root@]# /tmp/a.pl
Illegal division by zero at ./a.pl line 33.
would like to have the output of
a 2 2 30.00
c 1 1 60.00
b 1 0 0.00
Thanks
---------- Post updated at 12:38 PM ---------- Previous update was at 11:54 AM ----------
from Picking Up Perl - Associative Arrays (Hashes)
i can use each method to for loop its value.
Problem solved, but seek for more good way of doing that.
#!/usr/bin/perl
use strict;
use warnings;
my @sorted=();
my @tmp;
my $FA;
my @F;
my %q;
my %qq;
my %qqq;
open($FA, "/tmp/a.txt") or die "$!";
while(<$FA>) {
chomp;
@tmp=split(/\;/, $_);
push @sorted, "$_";
}
close $FA;
foreach (@sorted) {
@F = split(/\;/, $_);
$q{"$F[1]"} += 1;
if ($F[2] > 0) {
$qq{"$F[1]"} += 1;
} else {
$qq{"$F[1]"} += 0;
}
}
foreach my $key (keys %qq) {
if ($qq{$key} == 0) {
$qqq{$key} = "0.00";
} else {
$qqq{$key} = 60/$qq{$key};
}
}
print map "$_\t$q{$_}\t$qq{$_}\t".sprintf ("%.2f",$qqq{$_})."\n", sort { $q{$b} <=> $q{$a} } keys %q;