更新:编辑哈希数组内容



在我的数组中,我得到了如下内容;也就是说,格式如下:

Monday, June 12

我想摆脱

Monday,<---n.b.:这个逗号后面有一个空格。

部分。我习惯做的只是重新生成标签,然后像这样过滤它们:

foreach my $temp (@bad_array)
{
push @good_array, $temp =~ /MY BEAUTIFUL PATTERN/g;
}

这给了我一个主意。我想我可以这么做,但用符号修改一下代码

->as_text

[$i]

不知怎么地到处撒。但不确定是怎么回事。也许有一种明显的方法可以更有效地做到这一点。。。以下是@bad_array先生的背景故事:

my $tree_of_knowledge = HTML::Tree->new();
$tree_of_knowledge->parse($documentary);
my @bad_array = $tree_of_knowledge->look_down(
_tag => 'DESPAIR',
bgcolor => 'A VERY SAD COLOR'
);

哦,还有,以下是@bad_array最终发生的事情:

my $happy_time = Time::Piece->strptime($bad_array[0]->as_text . " " . $year, $format);

也许可以修改:

foreach ( @bad_array )
{
print $_->as_text, "n";
}

就是这样吗

foreach $temp( @bad_array )
{
push @good_array, $temp->as_text =~ /MY BEAUTIFUL PATTERN/g;
#THIS IS SIMPLY RIDICULOUS
}

还是这个

foreach( keys %hash ) {
$hash{$_} =~ s/something/something_else/g;
}

(来源:http://www.perlmonks.org/?node_id=477712)

也许是这个

foreach (values %hash) { s/[A-Z][a-z]*, //g  };

my %hash = @bad_array;在哪里,对吧?不,不可能,那是在复制。。。


结果如下:

#!/usr/bin/perl -w
use strict;
use LWP::Simple;
use Time::Piece;
use HTML::Tree;
print "Enter either "today" or a date (January 01 14): ";
my $userdate = <STDIN>;
chomp $userdate;
exit 0 if ($userdate eq "");
my $url0 = 'http://www.registrar.ucla.edu/calendar/acadcal13.htm';
my $url1 = 'http://www.registrar.ucla.edu/calendar/acadcal14.htm';
my $url2 = 'http://www.registrar.ucla.edu/calendar/acadcal15.htm';
my $url3 = 'http://www.registrar.ucla.edu/calendar/acadcal16.htm';
my $url4 = 'http://www.registrar.ucla.edu/calendar/acadcal17.htm';
my $url5 = 'http://www.registrar.ucla.edu/calendar/sumcal.htm';
my $document0 = get($url0);
my $document1 = get($url1);
my $document2 = get($url2);
my $document3 = get($url3);
my $document4 = get($url4);
my $document5 = get($url5);
my $tree0 = HTML::Tree->new();
$tree0->parse($document0);
my @juice_box0 = $tree0->look_down(
_tag => 'td',
bgcolor => '#FAE9F7'
);
my $tree1 = HTML::Tree->new();
$tree1->parse($document1);
my @juice_box1 = $tree1->look_down(
_tag => 'td',
bgcolor => '#FAE9F7'
);
my $tree2 = HTML::Tree->new();
$tree2->parse($document2);
my @juice_box2 = $tree2->look_down(
_tag => 'td',
bgcolor => '#FAE9F7'
);
my $tree3 = HTML::Tree->new();
$tree3->parse($document3);
my @juice_box3 = $tree3->look_down(
_tag => 'td',
bgcolor => '#FAE9F7'
);
my $tree4 = HTML::Tree->new();
$tree4->parse($document4);
my @juice_box4 = $tree4->look_down(
_tag => 'td',
bgcolor => '#FAE9F7'
);
my $tree5 = HTML::Tree->new();
$tree5->parse($document5);
my @juice_box5 = $tree5->look_down( #IT'S GOT 30 ELEMENTS
_tag => 'td',
width => '140'
);
my $date_tree = HTML::Tree->new();
$date_tree->parse($document5);
my @nectar_pod = $date_tree->look_down(
_tag => 'h4'
);
foreach ( @juice_box0 )
{
print $_->as_text, "n";
}
print "nn";
foreach ( @juice_box1 )
{
print $_->as_text, "n";
}
print "nn";
foreach ( @juice_box2 )
{
print $_->as_text, "n";
}
print "nn";
foreach ( @juice_box3 )
{
print $_->as_text, "n";
}
print "nn";
foreach ( @juice_box4 )
{
print $_->as_text, "n";
}
print "nn";
foreach ( @juice_box5 )
{
print $_->as_text, "n";
}
my $input_format = '%A, %B %d %y';
my $output_format = '%B %d %y';
#WHAT IS WRONG WITH THESE DATES? THEY ARE NOT PARSING...
my $t0 = Time::Piece->strptime($juice_box0[1]->as_text . ' ' . 13, $input_format)->strftime($output_format); #13
my $t1 = Time::Piece->strptime($juice_box0[5]->as_text . ' ' . 13, $input_format)->strftime($output_format); #13
my $t2 = Time::Piece->strptime($juice_box0[7]->as_text . ' ' . 14, $input_format)->strftime($output_format); #14
my $t3 = Time::Piece->strptime($juice_box0[11]->as_text . ' ' . 14, $input_format)->strftime($output_format); #14
my $t4 = Time::Piece->strptime($juice_box0[13]->as_text . ' ' . 14, $input_format)->strftime($output_format); #14
my $t5 = Time::Piece->strptime($juice_box0[17]->as_text . ' ' . 14, $input_format)->strftime($output_format); #14
my $u0 = Time::Piece->strptime($juice_box1[1]->as_text . ' ' . 14, $input_format)->strftime($output_format); #14
my $u1 = Time::Piece->strptime($juice_box1[5]->as_text . ' ' . 14, $input_format)->strftime($output_format); #14
my $u2 = Time::Piece->strptime($juice_box1[7]->as_text . ' ' . 15, $input_format)->strftime($output_format); #15
my $u3 = Time::Piece->strptime($juice_box1[11]->as_text . ' ' . 15, $input_format)->strftime($output_format); #15
my $u4 = Time::Piece->strptime($juice_box1[13]->as_text . ' ' . 15, $input_format)->strftime($output_format); #15
my $u5 = Time::Piece->strptime($juice_box1[17]->as_text . ' ' . 15, $input_format)->strftime($output_format); #15
my $v0 = Time::Piece->strptime($juice_box2[1]->as_text . ' ' . 15, $input_format)->strftime($output_format);
my $v1 = Time::Piece->strptime($juice_box2[5]->as_text . ' ' . 15, $input_format)->strftime($output_format);
my $v2 = Time::Piece->strptime($juice_box2[7]->as_text . ' ' . 16, $input_format)->strftime($output_format);
my $v3 = Time::Piece->strptime($juice_box2[11]->as_text . ' ' . 16, $input_format)->strftime($output_format);
my $v4 = Time::Piece->strptime($juice_box2[13]->as_text . ' ' . 16, $input_format)->strftime($output_format);
my $v5 = Time::Piece->strptime($juice_box2[17]->as_text . ' ' . 16, $input_format)->strftime($output_format);
my $w0 = Time::Piece->strptime($juice_box3[1]->as_text . ' ' .  16, $input_format)->strftime($output_format);
my $w1 = Time::Piece->strptime($juice_box3[5]->as_text . ' ' . 16, $input_format)->strftime($output_format);
my $w2 = Time::Piece->strptime($juice_box3[7]->as_text . ' ' . 17, $input_format)->strftime($output_format);
my $w3 = Time::Piece->strptime($juice_box3[11]->as_text . ' ' . 17, $input_format)->strftime($output_format);
my $w4 = Time::Piece->strptime($juice_box3[13]->as_text . ' ' . 17, $input_format)->strftime($output_format);
my $w5 = Time::Piece->strptime($juice_box3[17]->as_text . ' ' . 17, $input_format)->strftime($output_format);
my $x0 = Time::Piece->strptime($juice_box4[1]->as_text . ' ' . 17, $input_format)->strftime($output_format);
my $x1 = Time::Piece->strptime($juice_box4[5]->as_text . ' ' . 17, $input_format)->strftime($output_format);
my $x2 = Time::Piece->strptime($juice_box4[7]->as_text . ' ' . 18, $input_format)->strftime($output_format);
my $x3 = Time::Piece->strptime($juice_box4[11]->as_text . ' ' . 18, $input_format)->strftime($output_format);
my $x4 = Time::Piece->strptime($juice_box4[13]->as_text . ' ' . 18, $input_format)->strftime($output_format);
my $x5 = Time::Piece->strptime($juice_box4[17]->as_text . ' ' . 18, $input_format)->strftime($output_format);
my $y0 = Time::Piece->strptime($juice_box5[0]->as_text . ' ' . 12, $output_format);
my $y1 = Time::Piece->strptime($juice_box5[9]->as_text . ' ' . 12, $output_format);
my $y2 = Time::Piece->strptime($juice_box5[10]->as_text . ' ' . 13, $output_format);
my $y3 = Time::Piece->strptime($juice_box5[19]->as_text . ' ' . 13, $output_format);
my $y4 = Time::Piece->strptime($juice_box5[20]->as_text . ' ' . 14, $output_format);
my $y5 = Time::Piece->strptime($juice_box5[29]->as_text . ' ' . 14, $output_format);
my $date;
#THESE NEVER PRINT.
print "nn";
print $juice_box4[1]->as_text . ' ' . 17;
print "nn";
print $juice_box5[29]->as_text . ' ' . 14;
print "nn";
if( $userdate == 'today' ) # WHY IS THIS NOT RIGHT? I GET AN ERROR HERE.
{
$date = localtime->strftime('%B %d %y'); #USED TO HAVE %A, 
}
else
{
$date = Time::Piece->strptime($userdate, $input_format)->strftime($output_format);
}
if( ($t0->julian_day <= $date && $date <= $t1->julian_day) || ($u0->julian_day <= $date && $date <= $u1->julian_day) || ($v0->julian_day <= $date && $date <= $v1->julian_day) || ($w0->julian_day <= $date && $date <= $w1->julian_day) )
{
print "You are in the Fall Quarter. n";
}
elsif( ($t2->julian_day <= $date && $date <= $t3->julian_day) || ($u2->julian_day <= $date && $date <= $u3->julian_day) || ($v2->julian_day <= $date && $date <= $v3->julian_day) || ($w2->julian_day <= $date && $date <= $w3->julian_day) )
{
print "You are in the Winter Quarter. n";
}
elsif( ($t4->julian_day <= $date && $date <= $t5->julian_day) || ($u4->julian_day <= $date && $date <= $u5->julian_day) || ($v4->julian_day <= $date && $date <= $v5->julian_day) || ($w4->julian_day <= $date && $date <= $w5->julian_day) )
{
print "You are in the Spring Quarter. n";
}
elsif( ($y0->julian_day <= $date && $date <= $y1->julian_day) || ($y2->julian_day <= $date && $date <= $y3->julian_day) || ($y4->julian_day <= $date && $date <= $y5->julian_day)  )
{
print "You are in the Summer Quarter. n";
}
else
{
print "You are on break. n";
}

您可以这样做:

my @good = map { s/Monday,//; $_ } @bad_array;

使用Time::Piece的strftime方法重新格式化日期:

use Time::Piece;
my $input_format  = '%a, %b %d %Y';
my $output_format = '%b %d %y';
my $year       = '2014';
my $input_date = "Monday, June 12 $year";
my $output_date = Time::Piece->strptime( $input_date, $input_format )->strftime( $output_format );
print $output_date;

在您的情况下:

my $happy_time = Time::Piece->strptime( $bad_array[0]->as_text . " " . $year, $format )->strftime( '%b %d %y' );

相关内容

  • 没有找到相关文章

最新更新