更新:编辑哈希数组内容

时间:2014-02-14 05:07:02

标签: arrays perl hash tree html-tree

在我的数组中,我有一些看起来像这样的东西;也就是说,格式是这样的:

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 "\n\n";
foreach ( @juice_box1 )
{
        print $_->as_text, "\n";
}
print "\n\n";
foreach ( @juice_box2 )
{
        print $_->as_text, "\n";
}
print "\n\n";
foreach ( @juice_box3 )
{
        print $_->as_text, "\n";
}
print "\n\n";
foreach ( @juice_box4 )
{
        print $_->as_text, "\n";
}
print "\n\n";
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 "\n\n";
print $juice_box4[1]->as_text . ' ' . 17;
print "\n\n";
print $juice_box5[29]->as_text . ' ' . 14;
print "\n\n";

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";
}

2 个答案:

答案 0 :(得分:0)

你可以这样做:

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

答案 1 :(得分:0)

使用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' );