我是Perl的新手,但我听说它对于解析文件很有用,所以我想过给它一个旋转.
我有一个文本文件,其中包含以下示例信息:
High school is used in some
parts of the world, particularly in
Scotland, North America and Oceania to
describe an institution that provides
all or part of secondary education.
The term "high school" originated in
Scotland with the world's oldest being
the Royal High School (Edinburgh) in
1505.
The Royal High School was used as a
model for the first public high school
in the United States, the English High
School founded in Boston,
Massachusetts, in 1821. The precise
stage of schooling provided by a high
school differs from country to
country, and may vary within the same
jurisdiction. In all of New Zealand
and Malaysia along with parts of
Australia and Canada, high school is
synonymous with secondary school, and
encompasses the entire secondary stage
of education.
======================================
Grade1 87.43%
Grade2 84.30%
Grade3 83.00%
=====================================
Run Code Online (Sandbox Code Playgroud)
我想解析文件,只获取数字信息.我看了正则表达式,我想我会用类似的东西
if (m/^%/) {
do something
}
else {
skip the line
}
Run Code Online (Sandbox Code Playgroud)
但是,我真正想做的是跟踪左边的变量并将数值存储在该变量中.因此,在解析文件之后,我真的希望有以下变量来存储%值.原因是,我想创建一个不同等级的饼图/条形图.
等级1 = 87.43等级2 = 84.30
...
你能提出我应该看的方法吗?
你需要一个正则表达式.像下面这样的东西应该工作
while (<>) {
/(Grade[0-9]+)\s*([0-9]+\.[0-9]+)/;
$op{$1} = $2;
}
Run Code Online (Sandbox Code Playgroud)
作为过滤器.该op散列将存储等级名称和分数.这比自动实例化变量更可取.