#!/usr/bin/perl -w
# 1:two songs per artist,tops
# 2:the total music time must be at least 75 minutes but no greater than 80 minutes
# 3:the script should report back the name of the artist ,the name of the song ,and the length of the song in the format m:ss
# 4: at the end of the list ,you must then report the total dics time,also using the format m:ss.
# 5: you also need to sort the playlist alphabetically by artist name;
# 6: Total music time: 77:11
use strict;
my @result="";; #存放结果的数组。
my @temp; #临时数组
my $fh;
my $myrand; # 获得一个随机数,取数据。
open $fh, " < songlist.csv";
@temp=<$fh>;
my - 1 ;
print $allarray;
close($fh);
my %name; # 存贮艺术家的名 key:name,value:数量
my $time=0 ; # 存贮时间的长度 [4500,4800]
my $minute;
my $second;
my $realtime=0;
my $curname; # 获得记录的名字
my $curnum=0; # 当前艺术家的数量
while ($realtime < 4500)
{
$myrand = int (rand($allarray)+0);
my $current=$temp[$myrand];
$curname = (split /,/,$current)[0]; # 获得名字与时间
$time=(split /,/,$current)[2];
$minute=(split /:/,$time)[0];
$second=(split /:/,$time)[1];
$realtime+=$minute*60+$second; # 时间转换为秒来计算
if ($realtime>4800) # 如果超出80分钟,就退回去重做
{
$realtime=$realtime-$minute*60+$second;
next;
}
if ($current =~ @result )
{
next;
}
elsif( exists $name{$curname} && $name{$curname}==2 ) # 如果在hash里已经有两条记录了,则退出重来。
{
next;
}
else
{
push (@result,$current);
$name{$curname}+=1;
}
}
my $real_minute= int($realtime / 60);
my $_real_second= $realtime % 60;
print "*********the Result is :**********\n\n";
print sort(@result);
printf "\nThe total time is: %d:%02d \n",$real_minute,$_real_second ;
print "\n*******************\n";
my $key;
my $value;
print "----------the artists in the hash is :\n";
while(($key,$value)=each(%name))
{print "$key,$value\n";}
#
# if($_real_second <10 )
#{
# printf "\nThe total time is: %d:0%d \n",$real_minute,$_real_second ;
#}
#else
#{
# printf "\nThe total time is: %d:%2d \n",$real_minute,$_real_second ;
#}
阅读(1043) | 评论(0) | 转发(0) |