我有一个关于Perl的Foreach循环和拆分的问题。
我想循环数组并将其拆分为name和value。
1.)我读取文件并保存为String。
2.)我将空行拆分并保存到Array中。
我的脚本:
#!/usr/bin/perl
use strict;
my $pathconfigfile = 'config.conf';
my @configline;
open(my $configfile, "<", $pathconfigfile);
local $/;
my @configdata = split("\n\n",<$configfile>);
#print $configdata[0], "\n";
#print $configdata[1], "\n";
#print $configdata[2], "\n";
foreach my $data (@configdata){
my @editing = split /#/, $data;
my ($name, $value) = @editing[0,1];
print $name "\n";
print $value "\n";
}
close $configfile;
配置文件:
Testingtttttttttttttttttttttttt
############################################
0987654345678909876MN09uz6t56789oiuhgölkjhgfr
0987654323456789098765fgnloiuztlkjhgfrtzuiknb
MegaMixoiuzt
############################################
09876543457890098765NSUDlkjhzgtfr67899ztz9098
098765435678987t87656789876567898765679097658
TESTINGPARTS
############################################
0987654567890098765hzzasza654567uhgdjdjfacdaa
9876545678987654mchfuiaq754567898765434567876
我的愿望结果:
$name = Testingtttttttttttttttttttttttt
$值= 0987654345678909876 MN 09 uz 6 t56789 oiuhgölkjhgfr 0987654323456789098765 fgnloiuztlkjhgfrtzuiknb
$name = MegaMixoiuzt
$value = 09876543457890098765NSUDlkjhzgtfr67899ztz9098 098765435678987t87656789876567909765
$name =测试部件
$value = 0987654567890098765hzzasza654567uhgdjdjfacdaa 9876545678987654mchfuiaq754567898765434567876
3条答案
按热度按时间jrcvhitl1#
应
其他改进:
8yoxcaq72#
也像这样没有foreach:
oyjwcjzk3#
您希望按多个
#
拆分,因此使用#+
+
匹配一次或多次。试试