Steen Suder <sfs_news_spam@suder.dk> writes:
> Sikkert et skolebogsproblem, men idag er jeg tom for ideer...
>
> Jeg vil udtage "Daily"-blokken i en MRTG-side (altså den med fem
> minutters gennemsnit) for at generere en ny side.
>
> Blokken starter med "<!-- Begin `Daily' Graph (5 Minute -->" og slutter
> med "<!-- End `Daily' Graph (5 Minute -->".
>
> Man kan jo selvfølgeligt iterere igennem filen og så sætte et flag for
> at man har fundet starten af blokken osv. osv.... et eller andet siger
> mig at det bare ikke er den indlysende måde at gøre det på.
>
> Nogen forslag/hints?
Nu skriver du ikke om der kan være interessant info på linierne med
Begin/End, så det vil jeg lige antage.
while (<HANDLE>) {
if (s/.*?<!-- Begin `Daily' Graph (5 Minute -->// .. s/<!-- End `Daily' Graph (5 Minute -->.*//) {
print;
}
}
perldoc perlop siger:
In scalar context, ".." returns a boolean value. The operator is
bistable, like a flip-flop, and emulates the line-range (comma) opera-
tor of sed, awk, and various editors. Each ".." operator maintains its
own boolean state. It is false as long as its left operand is false.
Once the left operand is true, the range operator stays true until the
right operand is true, AFTER which the range operator becomes false
again. It doesn't become false till the next time the range operator
is evaluated. It can test the right operand and become false on the
same evaluation it became true (as in awk), but it still returns true
once.
Hvis du har hele siden i en enkelt scalar, kan du nøjes med:
if ($page =~ /<!-- Begin `Daily' Graph (5 Minute -->(.*?)<!-- End `Daily' Graph (5 Minute -->/) {
print $1;
}
--
Lars Balker Rasmussen Consult::Perl
http://consult-perl.dk