加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何使用Perl解析文件的一部分?

发布时间:2020-12-15 23:23:24 所属栏目:大数据 来源:网络整理
导读:我是Perl的新手,但我听说它对于解析文件很有用,所以我想过给它一个旋转. 我有一个文本文件,其中包含以下示例信息: High school is used in someparts of the world,particularly inScotland,North America and Oceania todescribe an institution that prov
我是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%
=====================================

我想解析文件,只获取数字信息.一世
看着正则表达式,我想我会使用类似的东西

if (m/^%/) {
    do something
}
else {
    skip the line
}

但是,我真正想做的是跟踪变量
留下并将数值存储在该变量中.所以,之后
解析文件,我真的想拥有以下变量
将%值存储在其中.原因是,我想
创建不同等级的饼图/条形图.

Grade1 = 87.43
Grade2 = 84.30

你能提出我应该看的方法吗?

解决方法

你需要一个正则表达式.像下面这样的东西应该工作

while (<>) {
  /(Grade[0-9]+)s*([0-9]+.[0-9]+)/;
  $op{$1} = $2;
}

作为过滤器. op hash将存储成绩名称和分数.这比自动实例化变量更可取.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读