嗨,我是新的perl,我堅持一個問題。我正在創建哈希散列,因爲我想存儲幾行作爲值。我只在價值上唱歌。如何存儲散列值散列中的幾行
FILE.TXT
family(){
kapoor(){
member1(){
male(){
between : 0 to 5
person : 2
their name is bla & bla.
and bla bla......
blaa.....
}
male(){
between : 5 to 5
person : 20
their name is bla & bla bla.
and bla bla......
blaa.....
}
}
member2(){
male(){
between : 0 to 5
person : 2
their name is bla & bla.
and bla bla......
blaa.....
}
male(){
between : 5 to 5
person : 20
their name is bla & bla bla.
and bla bla......
blaa.....
}
}
}
rai(){
member1(){
male(){
between : 0 to 5
person : 2
their name is bla & bla.
and bla bla......
blaa.....
}
male(){
between : 5 to 5
person : 20
their name is bla & bla bla.
and bla bla......
blaa.....
}
}
member2(){
male(){
between : 0 to 5
person : 2
their name is bla & bla.
and bla bla......
blaa.....
}
male(){
between : 5 to 5
person : 20
their name is bla & bla bla.
and bla bla......
blaa.....
}
}
}
O/P應該是:
family=>{
kapoor => {
member1=>{
0 to 5=> person : 2
their name is bla & bla.
and bla bla......
blaa.....
}
member2=> {
5 to 5 => person : 20
their name is bla & bla bla.
and bla bla......
blaa.....
}
rai =>{
/...................etc
}
我得到的值只有一行,像
family=>{
kapoor => {
member1=>{
0 to 5=> person : 2
}
member2=> {
5 to 5 => person : 20
}
rai =>{
/...................etc
}
這裏是我的嘗試:
while(my$line=<$fh>)
{
if($line=~/$family_name_re/)
{
$family_name=$1;
$family_count=$cell_count+1;
}
elsif ($line=~/$member_re/)
{
$member = $1;
}
elsif ($line=~/$age_re/)
{
$age_range=$1;
$data{$family_name}{$member}{$age_range}=$line;
}
}
print Dumper(\%data);
print "Number of cell is $cell_count\n";
close ($fh);
我不知道如何分配多個行來創建值。我在分配價值的同時嘗試,但沒有奏效。
(3或任何數量的線,你需要)..像'$一號線= <$fh>','$ 2號線= <$fh>'等並在分配之前將它們與'$ line'連接起來 – Sundeep
您的輸入文件非常混亂:嵌套看起來不正確(member1是兩個男性)並且您忽略了男性分組。此外你的代碼是不完整的,因爲你不給我們正在使用的正則表達式。正如@spasic所說的,你應該在某個時候讀取更多的行,因爲目前你只能以行爲單位讀取/存儲文件中的數據。這是目前的核心問題。 – eballes
ebailes說,文件格式及其解析沒有多大意義。你的散列分配看起來很好,但是(除了不清楚'$ line'實際上是什麼)。以正確格式向我們顯示文件和正則表達式。 – zdim