[問題] hash of array 跟array of hash
如標題,請問這兩種差在哪
push @{$o{a}},"1";
push @{$o{b}} , "1";
push @{$o{a}} ,"2";
push @{$o{c}} , "3";
這應該是 hash of array吧?
腦中補文應該是長這樣:
hash o裡面有三個key分別是 a,b 跟 c
其中 a 對應的值 是一個陣列,內元素有 1 跟 2
b 對應的值 是一個陣列,內元素有 1
c 對應的值 也是一個陣列,內元素有 3
問題一:那array of hash 又是要怎麼表達,還是我根本搞顛倒邊= =
=====================
有個記事本內文長如下(這是假設的,實際上有6萬多筆):
math a
math b
math c
eng a
eng b
eng d
chi a
chi d
chi f
phy g
phy q
...*6萬筆
===============code====
#!/usr/bin/perl -w
@name =("a","b");
for $idx(0..$#name){
open (FH,"test.txt")||die"File open Error!\n";
while(<FH>){
chomp;
@array=split(/\t/);
if($array[1] eq $name[$idx]){
push @{$o{$array[0]}},$array[1];
}
}
close (FH);
}
for $ele(keys %o){
push(@out,"$ele\t".scalar @{$o{$ele}}."\n");
}
print @out;
======輸出====
C:\>perl play2.p
Use of uninitialized value in string eq at play2.p line 12, <FH> line 12.
Use of uninitialized value in string eq at play2.p line 12, <FH> line 12.
chi 1
math 2
eng 2
意思是說 我輸入a 跟 b 然後去記事本找第2欄有a跟b的,
然後把第一欄整理發現有三個群組有包含 a 或 b 這兩個元素。最後整理成
chi 有找到一個
math有找到兩個
eng 有找到兩個
我要問的問題是:我把while包在for裡面,原data有6萬多筆,每for一次就是scan 6萬次
有沒有甚麼辦法可以只開一次檔就能全做?這範例是for 兩次的
--
\ ∩──、 =====
\/ ● , \ ======
/\( ● ¯● |つ
| ╳_入_ノ ミ ︵ 用這種餌
(_/ ノ / / 也想讓本大爺上鉤…
/\___ノ_/ / ======
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.39.163.146