本文介绍了在Perl中获取多个值到哈希中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该在文本文件中读入多个值,例如:

  maclawty796 pts / 1 75-30- 120-13.lig Wed Oct 12 19:27  -  19:33 
maclawty796 pts / 1 75-30-120-13.lig Wed Oct 12 19:35 - 19:38
hturner pts / 1 tom-nilsons-macb Wed Oct 12 13:30 - 13:32
nnt pts / 2 99-59-5-115.ligh 10月11日星期二15:51 - 15:54

并将它们转换为当月的总计,例如:

  Ehowe Sep 145分钟
Ehowe 10月38分钟
maclawty796 9月240分钟
maclawty796 10月155分钟

到目前为止,我的代码是:

 $ file ='C:\Users \user\Desktop\perlintro\timelog.txt'; 
打开(TimeLog,$ file)或死掉Could not open timelog.txt;

use strict;

my $ username;
my $ months;
my $ time;
my $ minutes1;
my $ minutes2;
my $ seconds1;
my $ seconds2;
my $ oneLine;
my%hash =(); $(< TimeLog>){
$ oneLine = $ _;

; (($ username)= /([[a-y] *(\d | \w)*)/){
printf%-12s,$ username;

。 ((月))= /((1月)|(2月)|(3月)|(4月)|(5月)|(6月)|(7月)|
}

。 (8月)|(9月)|(10月)|(11月)|(12月))/){
printf(%-7s,$ months); (($分钟1,$秒1,$分钟2,$秒2)= /(\d\d):( \d\d)\s(


(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\') ;
printf(%-3s minutes \\\
,$ time);

$ b $为我的$用户名(键(%哈希)){
为我的$月(键(%{$ hash {$ username}})){
print($ username $ months $ hash {$ username} {$ months} \\\
);





$ p
$ b $ p $因此,单日,但我需要帮助,使它只能按月打印。

解决方案

试试这个:

 #!/ usr / bin / perl 

使用警告;
使用strict;

my%time_log; $(< DATA>){
chomp;
my($ user,undef,undef,undef,$ mon,undef,$ time1,undef,$ time2)= split; ($($ user),$ mons)
$ time_log {$ user} - > {$ mon} + = time_diff($ time1,$ time2)
}

=每个%time_log){
while(my($ mon,$ period)= each%$ mons){
print$ user $ mon $ period\\\
;
}
}

sub time_diff {
my($ time1,$ time2)= @_;

my($ hh1,$ mm1,$ hh2,$ mm2)= split /:/,$ time1:$ time2;

return($ hh2 - $ hh1)* 60 +($ mm2 - $ mm1);
}

__DATA__
maclawty796 pts / 1 75-30-120-13.lig Wed Oct 12 19:27 - 19:33
maclawty796 pts / 1 75 -30-120-13.lig Wed Oct 12 19:35 - 19:38
hturner pts / 1 tom-nilsons-macb Wed Oct 12 13:30 - 13:32
nnt pts / 2 99 -59-5-115.ligh 10月11日星期二15:51 - 15:54


I am supposed to read in a text file with multiple values such as:

maclawty796 pts/1 75-30-120-13.lig Wed Oct 12 19:27 - 19:33  
maclawty796 pts/1 75-30-120-13.lig Wed Oct 12 19:35 - 19:38  
hturner pts/1 tom-nilsons-macb Wed Oct 12 13:30 - 13:32  
nnt pts/2 99-59-5-115.ligh Tue Oct 11 15:51 - 15:54

and turn them into totals for the month, such as:

Ehowe Sep 145 minutes   
Ehowe Oct 38  minutes   
maclawty796 Sep 240  minutes    
maclawty796 Oct 155  minutes

So far my code is:

$file = 'C:\Users\user\Desktop\perlintro\timelog.txt';
open(TimeLog, $file) or die "Couldn't open timelog.txt";

use strict; my $username; my $months; my $time; my $minutes1; my $minutes2; my $seconds1; my $seconds2; my $oneLine; my %hash = (); while (<TimeLog>) { $oneLine=$_; if ( ($username) = /([[a-y]*(\d|\w)*)/) { printf "%-12s", $username; } if ( ($months) = /((Jan) | (Feb) | (Mar) | (Apr) | (May) | (Jun) | (Jul) | (Aug) | (Sep) | (Oct) | (Nov) | (Dec) )/ ) { printf ("%-7s", $months); } if ( ($minutes1, $seconds1, $minutes2, $seconds2) = /(\d\d):(\d\d)\s-\s(\d\d):(\d\d)/ ) { $time = ($minutes2 * 60 + $seconds2) - ($minutes1 * 60 + $seconds1); printf ("%-3s minutes\n", $time); } for my $username (keys(%hash)) { for my $months (keys(%{ $hash{$username} })) { print("$username $months $hash{$username}{$months}\n"); } } }

So, currently it prints every single day, but I need help to make it only print by month.

解决方案

Try this:

#!/usr/bin/perl

use warnings;
use strict;

my %time_log;
while (<DATA>) {
    chomp;
    my ($user, undef, undef, undef, $mon, undef, $time1, undef, $time2) = split;
    $time_log{$user}->{$mon} += time_diff($time1, $time2)
}

while (my ($user, $mons) = each %time_log) {
    while (my ($mon, $period) = each %$mons) {
        print "$user $mon $period\n";
    }
}

sub time_diff {
    my ($time1, $time2) = @_;

    my ($hh1, $mm1, $hh2, $mm2) = split /:/, "$time1:$time2";

    return ($hh2 - $hh1) * 60 + ($mm2 - $mm1);
}

__DATA__
maclawty796 pts/1 75-30-120-13.lig Wed Oct 12 19:27 - 19:33  
maclawty796 pts/1 75-30-120-13.lig Wed Oct 12 19:35 - 19:38  
hturner pts/1 tom-nilsons-macb Wed Oct 12 13:30 - 13:32  
nnt pts/2 99-59-5-115.ligh Tue Oct 11 15:51 - 15:54

这篇关于在Perl中获取多个值到哈希中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 13:41